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.autofill.app.commoncases; 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.view.View; 23 import android.view.autofill.AutofillManager; 24 import android.widget.ArrayAdapter; 25 import android.widget.EditText; 26 import android.widget.Spinner; 27 28 import com.example.android.autofill.app.R; 29 import com.example.android.autofill.app.WelcomeActivity; 30 31 import java.util.Calendar; 32 33 public class CreditCardSpinnersActivity extends AppCompatActivity { 34 35 private static final int CC_EXP_YEARS_COUNT = 5; 36 37 private final String[] years = new String[CC_EXP_YEARS_COUNT]; 38 39 private Spinner mCcExpirationDaySpinner; 40 private Spinner mCcExpirationMonthSpinner; 41 private Spinner mCcExpirationYearSpinner; 42 private EditText mCcCardNumber; 43 private EditText mCcSecurityCode; 44 45 @Override onCreate(Bundle savedInstanceState)46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.credit_card_spinners_activity); 49 mCcExpirationDaySpinner = findViewById(R.id.expirationDay); 50 mCcExpirationMonthSpinner = findViewById(R.id.expirationMonth); 51 mCcExpirationYearSpinner = findViewById(R.id.expirationYear); 52 mCcCardNumber = findViewById(R.id.creditCardNumberField); 53 mCcSecurityCode = findViewById(R.id.creditCardSecurityCode); 54 55 // Create an ArrayAdapter using the string array and a default spinner layout 56 ArrayAdapter<CharSequence> dayAdapter = ArrayAdapter.createFromResource 57 (this, R.array.day_array, android.R.layout.simple_spinner_item); 58 // Specify the layout to use when the list of choices appears 59 dayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 60 // Apply the adapter to the spinner 61 mCcExpirationDaySpinner.setAdapter(dayAdapter); 62 63 /* 64 R.array.month_array could be an array of Strings like "Jan", "Feb", "March", etc., and 65 the AutofillService would know how to autofill it. However, for the sake of keeping the 66 AutofillService simple, we will stick to a list of numbers (1, 2, ... 12) to represent 67 months; it makes it much easier to generate fake autofill data in the service that can still 68 autofill this spinner. 69 */ 70 ArrayAdapter<CharSequence> monthAdapter = ArrayAdapter.createFromResource( 71 this, R.array.month_array, android.R.layout.simple_spinner_item); 72 // Adapter created from resource has getAutofillOptions() implemented by default. 73 monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 74 mCcExpirationMonthSpinner.setAdapter(monthAdapter); 75 76 int year = Calendar.getInstance().get(Calendar.YEAR); 77 for (int i = 0; i < years.length; i++) { 78 years[i] = Integer.toString(year + i); 79 } 80 // Since the years Spinner uses a custom adapter, it needs to implement getAutofillOptions. 81 mCcExpirationYearSpinner.setAdapter( 82 new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years) { 83 @Override 84 public CharSequence[] getAutofillOptions() { 85 return years; 86 } 87 }); 88 findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() { 89 @Override 90 public void onClick(View v) { 91 submit(); 92 } 93 }); 94 findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() { 95 @Override 96 public void onClick(View v) { 97 AutofillManager afm = getSystemService(AutofillManager.class); 98 if (afm != null) { 99 afm.cancel(); 100 } 101 resetFields(); 102 } 103 }); 104 } 105 resetFields()106 private void resetFields() { 107 mCcExpirationDaySpinner.setSelection(0); 108 mCcExpirationMonthSpinner.setSelection(0); 109 mCcExpirationYearSpinner.setSelection(0); 110 mCcCardNumber.setText(""); 111 mCcSecurityCode.setText(""); 112 } 113 114 /** 115 * Launches new Activity and finishes, triggering an autofill save request if the user entered 116 * any new data. 117 */ submit()118 private void submit() { 119 Intent intent = WelcomeActivity.getStartActivityIntent(CreditCardSpinnersActivity.this); 120 startActivity(intent); 121 finish(); 122 } 123 } 124