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