• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.antipatterns;
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 import com.example.android.autofill.app.R;
31 import com.example.android.autofill.app.WelcomeActivity;
32 import com.example.android.autofill.app.view.widget.InfoButton;
33 
34 import static com.example.android.autofill.app.Util.TAG;
35 
36 public class CallbackLessAutoCompleteSignInActivity extends AppCompatActivity {
37     private AutoCompleteTextView mUsernameAutoCompleteField;
38     private TextView mPasswordField;
39     private TextView mLoginButton;
40     private TextView mClearButton;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45 
46         setContentView(R.layout.login_with_autocomplete_activity);
47 
48         TextView title = findViewById(R.id.standard_login_header);
49         title.setText(R.string.navigation_button_anti_pattern_callbackless_autocomplete_login_label);
50 
51         InfoButton info = findViewById(R.id.imageButton);
52         info.setInfoText(getString(R.string.anti_pattern_callbackless_autocomplete_login_info));
53 
54 
55         mLoginButton = findViewById(R.id.login);
56         mClearButton = findViewById(R.id.clear);
57         mUsernameAutoCompleteField = findViewById(R.id.usernameField);
58         mPasswordField = findViewById(R.id.passwordField);
59         mLoginButton.setOnClickListener((v) -> login());
60         mClearButton.setOnClickListener((v) -> {
61             AutofillManager afm = getSystemService(AutofillManager.class);
62             if (afm != null) {
63                 afm.cancel();
64             }
65             resetFields();
66         });
67         ArrayAdapter<CharSequence> mockAutocompleteAdapter = ArrayAdapter.createFromResource
68                 (this, R.array.mock_autocomplete_sign_in_suggestions,
69                         android.R.layout.simple_dropdown_item_1line);
70         mUsernameAutoCompleteField.setAdapter(mockAutocompleteAdapter);
71         mUsernameAutoCompleteField.setThreshold(1);
72 
73         // Show it right away
74         mUsernameAutoCompleteField.setOnFocusChangeListener((v, hasFocus) -> {
75             if (hasFocus) {
76                 mUsernameAutoCompleteField.showDropDown();
77             }
78         });
79     }
80 
resetFields()81     private void resetFields() {
82         mUsernameAutoCompleteField.setText("");
83         mPasswordField.setText("");
84     }
85 
86     /**
87      * Emulates a login action.
88      */
login()89     private void login() {
90         String username = mUsernameAutoCompleteField.getText().toString();
91         String password = mPasswordField.getText().toString();
92         boolean valid = isValidCredentials(username, password);
93         if (valid) {
94             Intent intent = WelcomeActivity.getStartActivityIntent(CallbackLessAutoCompleteSignInActivity.this);
95             startActivity(intent);
96             finish();
97         } else {
98             Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show();
99         }
100     }
101 
102     /**
103      * Dummy implementation for demo purposes. A real service should use secure mechanisms to
104      * authenticate users.
105      */
isValidCredentials(String username, String password)106     public boolean isValidCredentials(String username, String password) {
107         return username != null && password != null && username.equals(password);
108     }
109 }