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