aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework
diff options
context:
space:
mode:
authorFelipe Leme <felipeal@google.com>2018-09-24 18:02:57 -0700
committerFelipe Leme <felipeal@google.com>2018-09-25 14:02:03 -0700
commit0c6c93dc1003eeecb7321862a71913523ef8be52 (patch)
treed7ddf045bd9667d04c88db8190dc94af51ba13c8 /input/autofill/AutofillFramework
parent856d28bb1e59b5d39899fa4795045267280dd20d (diff)
downloadandroid-0c6c93dc1003eeecb7321862a71913523ef8be52.tar.gz
Added antipatterns.BadViewStructureCreationSignInActivity activity.
This is a simple login page where the view structure is created onStart() instead of onCreate(), which causes autofill to misbehave when the autofill service requires authentication. Bug: 114236837 Bug: 111644535 Test: ./gradlew :Application:installDebug Test: manual verification Change-Id: I8761a3f71ceec44aefd7378da3329643ef015ce4
Diffstat (limited to 'input/autofill/AutofillFramework')
-rw-r--r--input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml2
-rw-r--r--input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/antipatterns/BadViewStructureCreationSignInActivity.java102
-rw-r--r--input/autofill/AutofillFramework/Application/src/main/res/layout/fragment_edge_cases.xml11
-rw-r--r--input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml5
4 files changed, 120 insertions, 0 deletions
diff --git a/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml b/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml
index 7c184686..1fe0b1e4 100644
--- a/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml
+++ b/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml
@@ -52,6 +52,8 @@
<activity android:name="com.example.android.autofill.app.edgecases.MultipleStepsSignInActivity" />
<activity android:name="com.example.android.autofill.app.edgecases.MultipleStepsCreditCardActivity" />
<activity android:name="com.example.android.autofill.app.commoncases.RecyclerViewActivity" />
+ <activity android:name="com.example.android.autofill.app.antipatterns.BadViewStructureCreationSignInActivity" />
+
</application>
</manifest>
diff --git a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/antipatterns/BadViewStructureCreationSignInActivity.java b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/antipatterns/BadViewStructureCreationSignInActivity.java
new file mode 100644
index 00000000..ddc57988
--- /dev/null
+++ b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/antipatterns/BadViewStructureCreationSignInActivity.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.autofill.app.antipatterns;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.view.autofill.AutofillManager;
+import android.widget.EditText;
+import android.widget.Toast;
+
+import com.example.android.autofill.app.R;
+import com.example.android.autofill.app.WelcomeActivity;
+
+/**
+ * This activity behaves the same as
+ * {@link com.example.android.autofill.app.commoncases.StandardSignInActivity}, except that it
+ * creates its view structure on {@link #onStart()}.
+ *
+ * <p>This is a bad pattern anyways&mdash;the view structure should be created on
+ * {@code onCreate()}&mdash;but it's aggravatted on autofill because when an autofill service
+ * requires authentication, the Android System launches a new activity to handle authentication
+ * using this activity's task. When the authentication acivity finishes, this activity is
+ * resumed, hence if {@link #onStart()} (or {@code onResume()}) re-generates the view structure,
+ * it invalidates the response sent by the autofill service, which triggers a new autofill request
+ * when a field is focused again.
+ */
+public class BadViewStructureCreationSignInActivity extends AppCompatActivity {
+
+ private EditText mUsernameEditText;
+ private EditText mPasswordEditText;
+
+ @Override
+ protected void onStart() {
+ super.onStart();
+
+ setContentView(R.layout.login_activity);
+ mUsernameEditText = findViewById(R.id.usernameField);
+ mPasswordEditText = findViewById(R.id.passwordField);
+ findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ login();
+ }
+ });
+ findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ AutofillManager afm = getSystemService(AutofillManager.class);
+ if (afm != null) {
+ afm.cancel();
+ }
+ resetFields();
+ }
+ });
+ }
+
+ private void resetFields() {
+ mUsernameEditText.setText("");
+ mPasswordEditText.setText("");
+ }
+
+ /**
+ * Emulates a login action.
+ */
+ private void login() {
+ String username = mUsernameEditText.getText().toString();
+ String password = mPasswordEditText.getText().toString();
+ boolean valid = isValidCredentials(username, password);
+ if (valid) {
+ Intent intent = WelcomeActivity
+ .getStartActivityIntent(BadViewStructureCreationSignInActivity.this);
+ startActivity(intent);
+ finish();
+ } else {
+ Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ /**
+ * Dummy implementation for demo purposes. A real service should use secure mechanisms to
+ * authenticate users.
+ */
+ public boolean isValidCredentials(String username, String password) {
+ return username != null && password != null && username.equalsIgnoreCase(password);
+ }
+}
diff --git a/input/autofill/AutofillFramework/Application/src/main/res/layout/fragment_edge_cases.xml b/input/autofill/AutofillFramework/Application/src/main/res/layout/fragment_edge_cases.xml
index c2d105e5..88a353d0 100644
--- a/input/autofill/AutofillFramework/Application/src/main/res/layout/fragment_edge_cases.xml
+++ b/input/autofill/AutofillFramework/Application/src/main/res/layout/fragment_edge_cases.xml
@@ -98,5 +98,16 @@
app:itemLogo="@drawable/ic_spinners_logo_24dp"
app:labelText="@string/navigation_button_multistep_cc_label"
app:destinationActivityName="com.example.android.autofill.app.edgecases.MultipleStepsCreditCardActivity"/>
+
+ <com.example.android.autofill.app.view.widget.NavigationItem
+ android:id="@+id/badViewStructureAntiPatternButton"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ app:imageColor="@android:color/holo_red_dark"
+ app:infoText="@string/anti_pattern_bad_view_structure_info"
+ app:itemLogo="@drawable/ic_disabled_black_24dp"
+ app:labelText="@string/navigation_button_anti_pattern_bad_view_structure_label"
+ app:destinationActivityName="com.example.android.autofill.app.antipatterns.BadViewStructureCreationSignInActivity"/>
+
</LinearLayout>
</ScrollView> \ No newline at end of file
diff --git a/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml b/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml
index 7f2c6465..fbd8a31d 100644
--- a/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml
+++ b/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml
@@ -31,6 +31,7 @@
<string name="navigation_button_multiple_partitions_label">Sample Page with Multiple Data Partitions</string>
<string name="navigation_button_web_view_login_label">Sample Login Using a WebView</string>
<string name="navigation_button_anti_pattern_credit_card_label">Sample Credit Card Anti Pattern</string>
+ <string name="navigation_button_anti_pattern_bad_view_structure_label">Bad View Structure Creation Anti Pattern</string>
<string name="navigation_button_multistep_signin_label">Multi-Step Sign In</string>
<string name="navigation_button_multistep_cc_label">Multi-Step Credit Card Check Out</string>
<string name="username_label">Username</string>
@@ -131,6 +132,10 @@
is through a View.AUTOFILL_TYPE_DATE value, which is what the other credit card sample
activities use.
</string>
+ <string name="anti_pattern_bad_view_structure_info">This is a simple login page where the view
+ structure is created onStart() instead of onCreate(), which causes autofill to misbehave
+ when the autofill service requires authentication.
+ </string>
<string name="multi_step_signin_info">
<!--TODO-->