1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.android.autofillframework.app
17 
18 import android.content.Context
19 import android.content.Intent
20 import android.os.Bundle
21 import android.support.v7.app.AppCompatActivity
22 import android.widget.ArrayAdapter
23 import com.example.android.autofillframework.R
24 import kotlinx.android.synthetic.main.credit_card_activity.clear
25 import kotlinx.android.synthetic.main.credit_card_activity.creditCardNumberField
26 import kotlinx.android.synthetic.main.credit_card_activity.expirationDay
27 import kotlinx.android.synthetic.main.credit_card_activity.expirationMonth
28 import kotlinx.android.synthetic.main.credit_card_activity.expirationYear
29 import kotlinx.android.synthetic.main.credit_card_activity.submit
30 import java.util.Calendar
31 
32 
33 class CreditCardActivity : AppCompatActivity() {
34 
35     private val CC_EXP_YEARS_COUNT = 5
36 
onCreatenull37     override fun onCreate(savedInstanceState: Bundle?) {
38         super.onCreate(savedInstanceState)
39         setContentView(R.layout.credit_card_activity)
40 
41         // Create an ArrayAdapter using the string array and a default spinner layout
42         expirationDay.adapter = ArrayAdapter.createFromResource(this, R.array.day_array,
43                 android.R.layout.simple_spinner_item).apply {
44             // Specify the layout to use when the list of choices appears
45             setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
46         }
47 
48         expirationMonth.adapter = ArrayAdapter.createFromResource(this, R.array.month_array,
49                 android.R.layout.simple_spinner_item).apply {
50             setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
51         }
52 
53         val year = Calendar.getInstance().get(Calendar.YEAR)
54 
55         val years = (0 until CC_EXP_YEARS_COUNT)
56                 .map { Integer.toString(year + it) }
57                 .toTypedArray<CharSequence>()
58 
59         expirationYear.adapter = object : ArrayAdapter<CharSequence?>(this,
60                 android.R.layout.simple_spinner_item, years) {
61             override fun getAutofillOptions() = years
62         }
63         submit.setOnClickListener { submitCcInfo() }
64         clear.setOnClickListener { resetFields() }
65     }
66 
resetFieldsnull67     private fun resetFields() {
68         creditCardNumberField.setText("")
69         expirationDay.setSelection(0)
70         expirationMonth.setSelection(0)
71         expirationYear.setSelection(0)
72     }
73 
74     /**
75      * Launches new Activity and finishes, triggering an autofill save request if the user entered
76      * any new data.
77      */
submitCcInfonull78     private fun submitCcInfo() {
79         startActivity(WelcomeActivity.getStartActivityIntent(this))
80         finish()
81     }
82 
83     companion object {
getStartActivityIntentnull84         fun getStartActivityIntent(context: Context) =
85                 Intent(context, CreditCardActivity::class.java)
86     }
87 }
88