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 android.autofillservice.cts; 17 18 import android.os.Bundle; 19 import android.util.Log; 20 import android.view.autofill.AutofillManager; 21 import android.widget.Button; 22 import android.widget.EditText; 23 import android.widget.TextView; 24 25 /** 26 * Simple activity that has an edit text and buttons to cancel or commit the autofill context. 27 */ 28 public class SimpleSaveActivity extends AbstractAutoFillActivity { 29 30 private static final String TAG = "SimpleSaveActivity"; 31 32 static final String ID_LABEL = "label"; 33 static final String ID_INPUT = "input"; 34 static final String ID_PASSWORD = "password"; 35 static final String ID_COMMIT = "commit"; 36 static final String TEXT_LABEL = "Label:"; 37 38 private static SimpleSaveActivity sInstance; 39 40 TextView mLabel; 41 EditText mInput; 42 EditText mPassword; 43 Button mCancel; 44 Button mCommit; 45 46 private boolean mAutoCommit = true; 47 private boolean mClearFieldsOnSubmit = false; 48 49 static SimpleSaveActivity getInstance() { 50 return sInstance; 51 } 52 53 static void finishIt(UiBot uiBot) { 54 if (sInstance != null) { 55 Log.d(TAG, "So long and thanks for all the fish!"); 56 sInstance.finish(); 57 uiBot.assertGoneByRelativeId(ID_LABEL, Timeouts.ACTIVITY_RESURRECTION); 58 } 59 } 60 61 public SimpleSaveActivity() { 62 sInstance = this; 63 } 64 65 @Override 66 protected void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 69 setContentView(R.layout.simple_save_activity); 70 71 mLabel = findViewById(R.id.label); 72 mInput = findViewById(R.id.input); 73 mPassword = findViewById(R.id.password); 74 mCancel = findViewById(R.id.cancel); 75 mCommit = findViewById(R.id.commit); 76 77 mCancel.setOnClickListener((v) -> getAutofillManager().cancel()); 78 mCommit.setOnClickListener((v) -> onCommit()); 79 } 80 81 private void onCommit() { 82 if (mClearFieldsOnSubmit) { 83 resetFields(); 84 } 85 if (mAutoCommit) { 86 Log.d(TAG, "onCommit(): calling AFM.commit()"); 87 getAutofillManager().commit(); 88 } else { 89 Log.d(TAG, "onCommit(): NOT calling AFM.commit()"); 90 } 91 } 92 93 private void resetFields() { 94 Log.d(TAG, "resetFields()"); 95 mInput.setText(""); 96 mPassword.setText(""); 97 } 98 99 /** 100 * Defines whether the activity should automatically call {@link AutofillManager#commit()} when 101 * the commit button is tapped. 102 */ 103 void setAutoCommit(boolean flag) { 104 mAutoCommit = flag; 105 } 106 107 /** 108 * Defines whether the activity should automatically clear its fields when submit is clicked. 109 */ 110 void setClearFieldsOnSubmit(boolean flag) { 111 mClearFieldsOnSubmit = flag; 112 } 113 114 FillExpectation expectAutoFill(String input) { 115 final FillExpectation expectation = new FillExpectation(input, null); 116 mInput.addTextChangedListener(expectation.mInputWatcher); 117 return expectation; 118 } 119 120 FillExpectation expectAutoFill(String input, String password) { 121 final FillExpectation expectation = new FillExpectation(input, password); 122 mInput.addTextChangedListener(expectation.mInputWatcher); 123 mPassword.addTextChangedListener(expectation.mPasswordWatcher); 124 return expectation; 125 } 126 127 final class FillExpectation { 128 private final OneTimeTextWatcher mInputWatcher; 129 private final OneTimeTextWatcher mPasswordWatcher; 130 131 private FillExpectation(String input, String password) { 132 mInputWatcher = new OneTimeTextWatcher("input", mInput, input); 133 mPasswordWatcher = password == null 134 ? null 135 : new OneTimeTextWatcher("password", mPassword, password); 136 } 137 138 void assertAutoFilled() throws Exception { 139 mInputWatcher.assertAutoFilled(); 140 if (mPasswordWatcher != null) { 141 mPasswordWatcher.assertAutoFilled(); 142 } 143 } 144 } 145 } 146