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 
26 public final class UsernameOnlyActivity extends AbstractAutoFillActivity {
27 
28     private static final String TAG = "UsernameOnlyActivity";
29 
30     private EditText mUsernameEditText;
31     private Button mNextButton;
32     private AutofillId mPasswordAutofillId;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         setContentView(getContentView());
38 
39         mUsernameEditText = findViewById(R.id.username);
40         mNextButton = findViewById(R.id.next);
41         mNextButton.setOnClickListener((v) -> next());
42     }
43 
getContentView()44     protected int getContentView() {
45         return R.layout.username_only_activity;
46     }
47 
focusOnUsername()48     public void focusOnUsername() {
49         syncRunOnUiThread(() -> mUsernameEditText.requestFocus());
50     }
51 
setUsername(String username)52     public void setUsername(String username) {
53         syncRunOnUiThread(() -> mUsernameEditText.setText(username));
54     }
55 
getUsernameAutofillId()56     public AutofillId getUsernameAutofillId() {
57         return mUsernameEditText.getAutofillId();
58     }
59 
60     /**
61      * Sets the autofill id of the password using the intent that launches the new activity, so it's
62      * set before the view strucutre is generated.
63      */
setPasswordAutofillId(AutofillId id)64     public void setPasswordAutofillId(AutofillId id) {
65         mPasswordAutofillId = id;
66     }
67 
next()68     public void next() {
69         final String username = mUsernameEditText.getText().toString();
70         Log.v(TAG, "Going to next screen as user " + username + " and aid " + mPasswordAutofillId);
71         final Intent intent = new Intent(this, PasswordOnlyActivity.class)
72                 .putExtra(PasswordOnlyActivity.EXTRA_USERNAME, username)
73                 .putExtra(PasswordOnlyActivity.EXTRA_PASSWORD_AUTOFILL_ID, mPasswordAutofillId);
74         startActivity(intent);
75         finish();
76     }
77 }
78