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