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 android.autofillservice.cts.activities;
17 
18 import android.autofillservice.cts.R;
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.view.autofill.AutofillId;
22 import android.widget.Button;
23 import android.widget.EditText;
24 import android.widget.TextView;
25 
26 public final class PasswordOnlyActivity extends AbstractAutoFillActivity {
27 
28     private static final String TAG = "PasswordOnlyActivity";
29 
30     static final String EXTRA_USERNAME = "username";
31     static final String EXTRA_PASSWORD_AUTOFILL_ID = "password_autofill_id";
32 
33     private TextView mWelcomeLabel;
34     private EditText mPasswordEditText;
35     private Button mLoginButton;
36     private String mUsername;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(getContentView());
42 
43         mWelcomeLabel = findViewById(R.id.welcome);
44         mPasswordEditText = findViewById(R.id.password);
45         mLoginButton = findViewById(R.id.login);
46         mLoginButton.setOnClickListener((v) -> login());
47 
48         mUsername = getIntent().getStringExtra(EXTRA_USERNAME);
49         final String welcomeMsg = "Welcome to the jungle, " + mUsername;
50         Log.v(TAG, welcomeMsg);
51         mWelcomeLabel.setText(welcomeMsg);
52         final AutofillId id = getIntent().getParcelableExtra(EXTRA_PASSWORD_AUTOFILL_ID);
53         if (id != null) {
54             Log.v(TAG, "Setting autofill id to " + id);
55             mPasswordEditText.setAutofillId(id);
56         }
57     }
58 
getContentView()59     protected int getContentView() {
60         return R.layout.password_only_activity;
61     }
62 
focusOnPassword()63     public void focusOnPassword() {
64         syncRunOnUiThread(() -> mPasswordEditText.requestFocus());
65     }
66 
setPassword(String password)67     public void setPassword(String password) {
68         syncRunOnUiThread(() -> mPasswordEditText.setText(password));
69     }
70 
login()71     public void login() {
72         final String password = mPasswordEditText.getText().toString();
73         Log.i(TAG, "Login as " + mUsername + "/" + password);
74         finish();
75     }
76 }
77