Home | History | Annotate | Download | only in commoncases
      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.annotation.Nullable;
     22 import android.support.v7.app.AppCompatActivity;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.autofill.AutofillManager;
     26 import android.widget.EditText;
     27 
     28 import com.example.android.autofill.app.R;
     29 import com.example.android.autofill.app.WelcomeActivity;
     30 import com.example.android.autofill.app.view.autofillable.CreditCardExpirationDatePickerView;
     31 
     32 import static com.example.android.autofill.app.Util.TAG;
     33 
     34 public class CreditCardDatePickerActivity extends AppCompatActivity {
     35 
     36     private CreditCardExpirationDatePickerView mCcExpDateView;
     37     private EditText mCcExpNumber;
     38     private EditText mCcSecurityCode;
     39 
     40     @Override
     41     protected void onCreate(@Nullable Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         setContentView(R.layout.credit_card_date_picker_activity);
     44         mCcExpDateView = findViewById(R.id.creditCardExpirationView);
     45         mCcExpNumber = 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         mCcExpDateView.reset();
     65     }
     66 
     67     private void resetFields() {
     68         mCcExpDateView.reset();
     69         mCcExpNumber.setText("");
     70         mCcSecurityCode.setText("");
     71     }
     72 
     73     public void showDatePickerDialog(View v) {
     74         if (v != mCcExpDateView) {
     75             Log.w(TAG, "showDatePickerDialog() called on invalid view: " + v);
     76             return;
     77         }
     78         mCcExpDateView.showDatePickerDialog(getSupportFragmentManager());
     79     }
     80 
     81 
     82     /**
     83      * Launches new Activity and finishes, triggering an autofill save request if the user entered
     84      * any new data.
     85      */
     86     private void submit() {
     87         Intent intent = WelcomeActivity.getStartActivityIntent(this);
     88         startActivity(intent);
     89         finish();
     90     }
     91 }
     92