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.v7.app.AppCompatActivity;
     22 import android.view.View;
     23 import android.view.autofill.AutofillManager;
     24 import android.widget.EditText;
     25 import android.widget.Toast;
     26 
     27 import com.example.android.autofill.app.R;
     28 import com.example.android.autofill.app.WelcomeActivity;
     29 
     30 public class StandardSignInActivity extends AppCompatActivity {
     31 
     32     private EditText mUsernameEditText;
     33     private EditText mPasswordEditText;
     34 
     35     @Override
     36     protected void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38 
     39         setContentView(R.layout.login_activity);
     40         mUsernameEditText = findViewById(R.id.usernameField);
     41         mPasswordEditText = findViewById(R.id.passwordField);
     42         findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
     43             @Override
     44             public void onClick(View v) {
     45                 login();
     46             }
     47         });
     48         findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
     49             @Override
     50             public void onClick(View v) {
     51                 AutofillManager afm = getSystemService(AutofillManager.class);
     52                 if (afm != null) {
     53                     afm.cancel();
     54                 }
     55                 resetFields();
     56             }
     57         });
     58     }
     59 
     60     private void resetFields() {
     61         mUsernameEditText.setText("");
     62         mPasswordEditText.setText("");
     63     }
     64 
     65     /**
     66      * Emulates a login action.
     67      */
     68     private void login() {
     69         String username = mUsernameEditText.getText().toString();
     70         String password = mPasswordEditText.getText().toString();
     71         boolean valid = isValidCredentials(username, password);
     72         if (valid) {
     73             Intent intent = WelcomeActivity.getStartActivityIntent(StandardSignInActivity.this);
     74             startActivity(intent);
     75             finish();
     76         } else {
     77             Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show();
     78         }
     79     }
     80 
     81     /**
     82      * Dummy implementation for demo purposes. A real service should use secure mechanisms to
     83      * authenticate users.
     84      */
     85     public boolean isValidCredentials(String username, String password) {
     86         return username != null && password != null && username.equalsIgnoreCase(password);
     87     }
     88 }
     89