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.edgecases;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.support.annotation.Nullable;
22 import android.support.v7.app.AppCompatActivity;
23 import android.view.View;
24 import android.view.autofill.AutofillManager;
25 import android.widget.EditText;
26 
27 import com.example.android.autofill.app.R;
28 import com.example.android.autofill.app.WelcomeActivity;
29 
30 public class CreditCardActivity extends AppCompatActivity {
31 
32     private EditText mCcExpDayView;
33     private EditText mCcExpMonthView;
34     private EditText mCcExpYearView;
35     private EditText mCcNumber;
36     private EditText mCcSecurityCode;
37 
38     @Override
onCreate(@ullable Bundle savedInstanceState)39     protected void onCreate(@Nullable Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.credit_card_activity);
42         mCcExpDayView = findViewById(R.id.expirationDay);
43         mCcExpMonthView = findViewById(R.id.expirationMonth);
44         mCcExpYearView = findViewById(R.id.expirationYear);
45         mCcNumber = findViewById(R.id.creditCardNumberField);
46         mCcSecurityCode = findViewById(R.id.creditCardSecurityCode);
47         findViewById(R.id.submitButton).setOnClickListener(new View.OnClickListener() {
48             @Override
49             public void onClick(View v) {
50                 submit();
51             }
52         });
53         findViewById(R.id.clearButton).setOnClickListener(new View.OnClickListener() {
54             @Override
55             public void onClick(View v) {
56                 AutofillManager afm = getSystemService(AutofillManager.class);
57                 if (afm != null) {
58                     afm.cancel();
59                 }
60                 resetFields();
61             }
62         });
63     }
64 
resetFields()65     private void resetFields() {
66         mCcExpDayView.setText("");
67         mCcExpMonthView.setText("");
68         mCcExpYearView.setText("");
69         mCcNumber.setText("");
70         mCcSecurityCode.setText("");
71     }
72 
73     /**
74      * Launches new Activity and finishes, triggering an autofill save request if the user entered
75      * any new data.
76      */
submit()77     private void submit() {
78         Intent intent = WelcomeActivity.getStartActivityIntent(this);
79         startActivity(intent);
80         finish();
81     }
82 }
83