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