Home | History | Annotate | Download | only in app
      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;
     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.view.View;
     23 import android.view.autofill.AutofillManager;
     24 import android.widget.Toast;
     25 
     26 /**
     27  * Activity that uses a virtual views for Username/Password text fields.
     28  */
     29 public class VirtualSignInActivity extends AppCompatActivity {
     30 
     31     private CustomVirtualView mCustomVirtualView;
     32     private AutofillManager mAutofillManager;
     33     private CustomVirtualView.Line mUsernameLine;
     34     private CustomVirtualView.Line mPasswordLine;
     35 
     36     public static Intent getStartActivityIntent(Context context) {
     37         Intent intent = new Intent(context, VirtualSignInActivity.class);
     38         return intent;
     39     }
     40 
     41     @Override
     42     protected void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44 
     45         setContentView(R.layout.virtual_login_activity);
     46 
     47         mCustomVirtualView = (CustomVirtualView) findViewById(R.id.custom_view);
     48 
     49         CustomVirtualView.Partition credentialsPartition =
     50                 mCustomVirtualView.addPartition(getString(R.string.partition_credentials));
     51         mUsernameLine = credentialsPartition.addLine("username", View.AUTOFILL_TYPE_TEXT,
     52                 getString(R.string.username_label),
     53                 "         ", false, View.AUTOFILL_HINT_USERNAME);
     54         mPasswordLine = credentialsPartition.addLine("password", View.AUTOFILL_TYPE_TEXT,
     55                 getString(R.string.password_label),
     56                 "         ", true, View.AUTOFILL_HINT_PASSWORD);
     57 
     58         findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
     59             @Override
     60             public void onClick(View view) {
     61                 login();
     62             }
     63         });
     64         findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
     65             @Override
     66             public void onClick(View view) {
     67                 resetFields();
     68                 mAutofillManager.cancel();
     69             }
     70         });
     71         mAutofillManager = getSystemService(AutofillManager.class);
     72     }
     73 
     74     private void resetFields() {
     75         mUsernameLine.reset();
     76         mPasswordLine.reset();
     77         mCustomVirtualView.postInvalidate();
     78     }
     79 
     80     /**
     81      * Emulates a login action.
     82      */
     83     private void login() {
     84         String username = mUsernameLine.getText().toString();
     85         String password = mPasswordLine.getText().toString();
     86         boolean valid = isValidCredentials(username, password);
     87         if (valid) {
     88             Intent intent = WelcomeActivity.getStartActivityIntent(VirtualSignInActivity.this);
     89             startActivity(intent);
     90             finish();
     91         } else {
     92             Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show();
     93         }
     94     }
     95 
     96     /**
     97      * Dummy implementation for demo purposes. A real service should use secure mechanisms to
     98      * authenticate users.
     99      */
    100     public boolean isValidCredentials(String username, String password) {
    101         return username != null && password != null && username.equals(password);
    102     }
    103 }
    104