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.annotation.NonNull; 22 import android.support.v7.app.AppCompatActivity; 23 import android.util.Log; 24 import android.view.View; 25 import android.view.autofill.AutofillManager; 26 import android.widget.ArrayAdapter; 27 import android.widget.AutoCompleteTextView; 28 import android.widget.TextView; 29 import android.widget.Toast; 30 31 import com.example.android.autofill.app.R; 32 import com.example.android.autofill.app.WelcomeActivity; 33 34 import static com.example.android.autofill.app.Util.TAG; 35 36 public class StandardAutoCompleteSignInActivity extends AppCompatActivity { 37 private AutoCompleteTextView mUsernameAutoCompleteField; 38 private TextView mPasswordField; 39 private TextView mLoginButton; 40 private TextView mClearButton; 41 private boolean mAutofillReceived = false; 42 private AutofillManager.AutofillCallback mAutofillCallback; 43 private AutofillManager mAutofillManager; 44 45 @Override 46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 setContentView(R.layout.login_with_autocomplete_activity); 50 51 mLoginButton = findViewById(R.id.login); 52 mClearButton = findViewById(R.id.clear); 53 mUsernameAutoCompleteField = findViewById(R.id.usernameField); 54 mPasswordField = findViewById(R.id.passwordField); 55 mLoginButton.setOnClickListener(new View.OnClickListener() { 56 @Override 57 public void onClick(View v) { 58 login(); 59 } 60 }); 61 mClearButton.setOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View v) { 64 AutofillManager afm = getSystemService(AutofillManager.class); 65 if (afm != null) { 66 afm.cancel(); 67 } 68 resetFields(); 69 } 70 }); 71 mAutofillCallback = new MyAutofillCallback(); 72 mAutofillManager = getSystemService(AutofillManager.class); 73 ArrayAdapter<CharSequence> mockAutocompleteAdapter = ArrayAdapter.createFromResource 74 (this, R.array.mock_autocomplete_sign_in_suggestions, 75 android.R.layout.simple_dropdown_item_1line); 76 mUsernameAutoCompleteField.setAdapter(mockAutocompleteAdapter); 77 } 78 79 @Override 80 protected void onResume() { 81 super.onResume(); 82 mAutofillManager.registerCallback(mAutofillCallback); 83 } 84 85 @Override 86 protected void onPause() { 87 super.onPause(); 88 mAutofillManager.unregisterCallback(mAutofillCallback); 89 } 90 91 private void resetFields() { 92 mUsernameAutoCompleteField.setText(""); 93 mPasswordField.setText(""); 94 } 95 96 /** 97 * Emulates a login action. 98 */ 99 private void login() { 100 String username = mUsernameAutoCompleteField.getText().toString(); 101 String password = mPasswordField.getText().toString(); 102 boolean valid = isValidCredentials(username, password); 103 if (valid) { 104 Intent intent = WelcomeActivity.getStartActivityIntent(StandardAutoCompleteSignInActivity.this); 105 startActivity(intent); 106 finish(); 107 } else { 108 Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show(); 109 } 110 } 111 112 /** 113 * Dummy implementation for demo purposes. A real service should use secure mechanisms to 114 * authenticate users. 115 */ 116 public boolean isValidCredentials(String username, String password) { 117 return username != null && password != null && username.equals(password); 118 } 119 120 private class MyAutofillCallback extends AutofillManager.AutofillCallback { 121 @Override 122 public void onAutofillEvent(@NonNull View view, int event) { 123 if (view instanceof AutoCompleteTextView) { 124 switch (event) { 125 case AutofillManager.AutofillCallback.EVENT_INPUT_UNAVAILABLE: 126 // no break on purpose 127 case AutofillManager.AutofillCallback.EVENT_INPUT_HIDDEN: 128 if (!mAutofillReceived) { 129 ((AutoCompleteTextView) view).showDropDown(); 130 } 131 break; 132 case AutofillManager.AutofillCallback.EVENT_INPUT_SHOWN: 133 mAutofillReceived = true; 134 ((AutoCompleteTextView) view).setAdapter(null); 135 break; 136 default: 137 Log.d(TAG, "Unexpected callback: " + event); 138 } 139 } 140 } 141 } 142 }