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 19 import android.content.Context 20 import android.content.Intent 21 import android.os.Bundle 22 import android.support.v7.app.AppCompatActivity 23 import android.widget.Toast 24 import com.example.android.autofillframework.R 25 import kotlinx.android.synthetic.main.login_activity.clear 26 import kotlinx.android.synthetic.main.login_activity.login 27 import kotlinx.android.synthetic.main.login_activity.passwordField 28 import kotlinx.android.synthetic.main.login_activity.usernameField 29 30 31 class StandardSignInActivity : AppCompatActivity() { 32 33 override fun onCreate(savedInstanceState: Bundle?) { 34 super.onCreate(savedInstanceState) 35 setContentView(R.layout.login_activity) 36 login.setOnClickListener { login() } 37 clear.setOnClickListener { resetFields() } 38 } 39 40 private fun resetFields() { 41 usernameField.setText("") 42 passwordField.setText("") 43 } 44 45 /** 46 * Emulates a login action. 47 */ 48 private fun login() { 49 val username = usernameField.text.toString() 50 val password = passwordField.text.toString() 51 val valid = isValidCredentials(username, password) 52 if (valid) { 53 val intent = WelcomeActivity.getStartActivityIntent(this@StandardSignInActivity) 54 startActivity(intent) 55 finish() 56 } else { 57 Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show() 58 } 59 } 60 61 /** 62 * Dummy implementation for demo purposes. A real service should use secure mechanisms to 63 * authenticate users. 64 */ 65 fun isValidCredentials(username: String, password: String): Boolean { 66 return username == password 67 } 68 69 companion object { 70 71 fun getStartActivityIntent(context: Context): Intent { 72 val intent = Intent(context, StandardSignInActivity::class.java) 73 return intent 74 } 75 } 76 }