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