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