diff options
Diffstat (limited to 'input')
8 files changed, 305 insertions, 45 deletions
diff --git a/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml b/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml index eb1f43c2..787a6dba 100644 --- a/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml +++ b/input/autofill/AutofillFramework/Application/src/main/AndroidManifest.xml @@ -16,9 +16,9 @@ --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="com.example.android.autofillframework" - android:versionCode="1" - android:versionName="1.0"> + package="com.example.android.autofillframework" + android:versionCode="1" + android:versionName="1.0"> <application android:allowBackup="true" @@ -36,14 +36,19 @@ </intent-filter> </activity> <activity - android:name=".app.LoginActivity" + android:name=".app.StandardSignInActivity" android:label="AF StandardLogin" - android:taskAffinity=".LoginActivity"> + android:taskAffinity=".StandardSignInActivity"> </activity> <activity - android:name=".app.VirtualLoginActivity" + android:name=".app.StandardAutoCompleteSignInActivity" + android:label="AF StandardLogin" + android:taskAffinity=".StandardAutoCompleteSignInActivity" /> + <activity + android:name=".app.VirtualSignInActivity" android:label="AF VirtualLogin" - android:taskAffinity=".VirtualLoginActivity"> + android:taskAffinity=".VirtualSignInActivity"> + </activity> <activity android:name=".app.WelcomeActivity" /> <activity diff --git a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.java b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.java index 4e7a6caa..cf76aee1 100644 --- a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.java +++ b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.java @@ -26,44 +26,37 @@ import com.example.android.autofillframework.R; /** * This is used to launch sample activities that showcase autofill. */ -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - findViewById(R.id.standardViewSignInButton).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - standardViewSignIn(); - } - }); - findViewById(R.id.virtualViewSignInButton).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - virtualViewSignIn(); - } - }); - findViewById(R.id.creditCardCheckoutButton).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - creditCardCheckout(); - } - }); + findViewById(R.id.standardViewSignInButton).setOnClickListener(this); + findViewById(R.id.virtualViewSignInButton).setOnClickListener(this); + findViewById(R.id.creditCardCheckoutButton).setOnClickListener(this); + findViewById(R.id.standardLoginWithAutoCompleteButton).setOnClickListener(this); } - private void creditCardCheckout() { - Intent intent = CreditCardActivity.getStartActivityIntent(this); - startActivity(intent); - } - - private void standardViewSignIn() { - Intent intent = LoginActivity.getStartActivityIntent(this); - startActivity(intent); - } - - private void virtualViewSignIn() { - Intent intent = VirtualLoginActivity.getStartActivityIntent(this); - startActivity(intent); + @Override + public void onClick(View view) { + Intent intent = null; + switch (view.getId()) { + case R.id.standardViewSignInButton: + intent = StandardSignInActivity.getStartActivityIntent(this); + break; + case R.id.virtualViewSignInButton: + intent = VirtualSignInActivity.getStartActivityIntent(this); + break; + case R.id.creditCardCheckoutButton: + intent = CreditCardActivity.getStartActivityIntent(this); + break; + case R.id.standardLoginWithAutoCompleteButton: + intent = StandardAutoCompleteSignInActivity.getStartActivityIntent(this); + break; + } + if (intent != null) { + startActivity(intent); + } } }
\ No newline at end of file diff --git a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/StandardAutoCompleteSignInActivity.java b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/StandardAutoCompleteSignInActivity.java new file mode 100644 index 00000000..d5ed88f0 --- /dev/null +++ b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/StandardAutoCompleteSignInActivity.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2017 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.autofillframework.app; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.view.autofill.AutofillManager; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; + +import com.example.android.autofillframework.R; + +import static com.example.android.autofillframework.CommonUtil.TAG; + +public class StandardAutoCompleteSignInActivity extends AppCompatActivity { + private AutoCompleteTextView mUsernameAutoCompleteField; + private TextView mPasswordField; + private Button mLoginButton; + private Button mClearButton; + private boolean mAutofillReceived = false; + private AutofillManager.AutofillCallback mAutofillCallback; + private AutofillManager mAutofillManager; + + public static Intent getStartActivityIntent(Context context) { + Intent intent = new Intent(context, StandardAutoCompleteSignInActivity.class); + return intent; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.login_with_autocomplete_activity); + + mLoginButton = findViewById(R.id.login); + mClearButton = findViewById(R.id.clear); + mUsernameAutoCompleteField = findViewById(R.id.usernameField); + mPasswordField = findViewById(R.id.passwordField); + mLoginButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + login(); + } + }); + mClearButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + resetFields(); + } + }); + mAutofillCallback = new MyAutofillCallback(); + mAutofillManager = getSystemService(AutofillManager.class); + ArrayAdapter<CharSequence> mockAutocompleteAdapter = ArrayAdapter.createFromResource + (this, R.array.mock_autocomplete_sign_in_suggestions, + android.R.layout.simple_dropdown_item_1line); + mUsernameAutoCompleteField.setAdapter(mockAutocompleteAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + mAutofillManager.registerCallback(mAutofillCallback); + } + + @Override + protected void onPause() { + super.onPause(); + mAutofillManager.unregisterCallback(mAutofillCallback); + } + + private void resetFields() { + mUsernameAutoCompleteField.setText(""); + mPasswordField.setText(""); + } + + /** + * Emulates a login action. + */ + private void login() { + String username = mUsernameAutoCompleteField.getText().toString(); + String password = mPasswordField.getText().toString(); + boolean valid = isValidCredentials(username, password); + if (valid) { + Intent intent = WelcomeActivity.getStartActivityIntent(StandardAutoCompleteSignInActivity.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.equals(password); + } + + private class MyAutofillCallback extends AutofillManager.AutofillCallback { + @Override + public void onAutofillEvent(View view, int event) { + super.onAutofillEvent(view, event); + if (view instanceof AutoCompleteTextView) { + switch (event) { + case AutofillManager.AutofillCallback.EVENT_INPUT_UNAVAILABLE: + // no break on purpose + case AutofillManager.AutofillCallback.EVENT_INPUT_HIDDEN: + if (!mAutofillReceived) { + ((AutoCompleteTextView) view).showDropDown(); + } + break; + case AutofillManager.AutofillCallback.EVENT_INPUT_SHOWN: + mAutofillReceived = true; + ((AutoCompleteTextView) view).setAdapter(null); + break; + default: + Log.d(TAG, "Unexpected callback: " + event); + } + } + } + } +}
\ No newline at end of file diff --git a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/LoginActivity.java b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/StandardSignInActivity.java index ec7ab581..9038e3da 100644 --- a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/LoginActivity.java +++ b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/StandardSignInActivity.java @@ -25,13 +25,13 @@ import android.widget.Toast; import com.example.android.autofillframework.R; -public class LoginActivity extends AppCompatActivity { +public class StandardSignInActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; public static Intent getStartActivityIntent(Context context) { - Intent intent = new Intent(context, LoginActivity.class); + Intent intent = new Intent(context, StandardSignInActivity.class); return intent; } @@ -69,7 +69,7 @@ public class LoginActivity extends AppCompatActivity { String password = mPasswordEditText.getText().toString(); boolean valid = isValidCredentials(username, password); if (valid) { - Intent intent = WelcomeActivity.getStartActivityIntent(LoginActivity.this); + Intent intent = WelcomeActivity.getStartActivityIntent(StandardSignInActivity.this); startActivity(intent); finish(); } else { diff --git a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.java b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/VirtualSignInActivity.java index 0c787090..3d23e3f8 100644 --- a/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.java +++ b/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/VirtualSignInActivity.java @@ -20,17 +20,19 @@ 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.Toast; import com.example.android.autofillframework.R; -public class VirtualLoginActivity extends AppCompatActivity { +public class VirtualSignInActivity extends AppCompatActivity { private CustomVirtualView mCustomVirtualView; + private AutofillManager mAutofillManager; public static Intent getStartActivityIntent(Context context) { - Intent intent = new Intent(context, VirtualLoginActivity.class); + Intent intent = new Intent(context, VirtualSignInActivity.class); return intent; } @@ -53,6 +55,17 @@ public class VirtualLoginActivity extends AppCompatActivity { resetFields(); } }); + mAutofillManager = getSystemService(AutofillManager.class); + } + + @Override + protected void onResume() { + super.onResume(); + } + + @Override + protected void onPause() { + super.onPause(); } private void resetFields() { @@ -67,7 +80,7 @@ public class VirtualLoginActivity extends AppCompatActivity { String password = mCustomVirtualView.getPasswordText().toString(); boolean valid = isValidCredentials(username, password); if (valid) { - Intent intent = WelcomeActivity.getStartActivityIntent(VirtualLoginActivity.this); + Intent intent = WelcomeActivity.getStartActivityIntent(VirtualSignInActivity.this); startActivity(intent); finish(); } else { diff --git a/input/autofill/AutofillFramework/Application/src/main/res/layout/activity_main.xml b/input/autofill/AutofillFramework/Application/src/main/res/layout/activity_main.xml index 26d0657d..b2510b07 100644 --- a/input/autofill/AutofillFramework/Application/src/main/res/layout/activity_main.xml +++ b/input/autofill/AutofillFramework/Application/src/main/res/layout/activity_main.xml @@ -30,6 +30,12 @@ android:text="@string/standard_view_sign_in" /> <Button + android:id="@+id/standardLoginWithAutoCompleteButton" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/standard_view_autocomplete_sign_in"/> + + <Button android:id="@+id/virtualViewSignInButton" android:layout_width="match_parent" android:layout_height="wrap_content" diff --git a/input/autofill/AutofillFramework/Application/src/main/res/layout/login_with_autocomplete_activity.xml b/input/autofill/AutofillFramework/Application/src/main/res/layout/login_with_autocomplete_activity.xml new file mode 100644 index 00000000..193387b8 --- /dev/null +++ b/input/autofill/AutofillFramework/Application/src/main/res/layout/login_with_autocomplete_activity.xml @@ -0,0 +1,93 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + * Copyright (C) 2017 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. +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/standardLoginLayout" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:focusable="true" + android:focusableInTouchMode="true" + android:orientation="vertical" + android:paddingBottom="@dimen/activity_vertical_margin" + android:paddingLeft="@dimen/activity_horizontal_margin" + android:paddingRight="@dimen/activity_horizontal_margin" + android:paddingTop="@dimen/activity_vertical_margin"> + + <RelativeLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center"> + + <TextView + android:id="@+id/usernameLabel" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginEnd="20dp" + android:importantForAutofill="no" + android:labelFor="@+id/usernameField" + android:text="@string/username_label" /> + + <AutoCompleteTextView + android:id="@+id/usernameField" + android:layout_width="200sp" + android:layout_height="wrap_content" + android:layout_alignBaseline="@+id/usernameLabel" + android:layout_toEndOf="@id/usernameLabel" + android:autofillHints="username" + android:inputType="textPersonName" /> + + <TextView + android:id="@+id/passwordLabel" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignStart="@+id/usernameLabel" + android:layout_below="@+id/usernameLabel" + android:layout_marginTop="20dp" + android:importantForAutofill="no" + android:text="@string/password_label" + android:labelFor="@+id/passwordField"/> + + <EditText + android:id="@+id/passwordField" + android:layout_width="200sp" + android:layout_height="wrap_content" + android:layout_alignBaseline="@+id/passwordLabel" + android:layout_alignStart="@+id/usernameField" + android:autofillHints="password" + android:inputType="textPassword" /> + </RelativeLayout> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginTop="20dp" + android:gravity="center" + android:orientation="horizontal"> + + <Button + android:id="@+id/clear" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Clear" /> + + <Button + android:id="@+id/login" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Login" /> + </LinearLayout> + +</LinearLayout>
\ 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 f9448a74..ffb8495a 100644 --- a/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml +++ b/input/autofill/AutofillFramework/Application/src/main/res/values/strings.xml @@ -22,6 +22,8 @@ <string name="password_label">Password</string> <string name="welcome_text">You have successfully signed in!</string> <string name="standard_view_sign_in">Sign in using standard views</string> + <string name="standard_view_autocomplete_sign_in">Sign in using standard views that + trigger AutoComplete dialogs when focused</string> <string name="virtual_view_sign_in">Sign in using virtual views</string> <string name="credit_card_checkout">Credit Card check out</string> <string name="autofill_sign_in_prompt">Tap to sign in.</string> @@ -104,4 +106,9 @@ <item>2024</item> </string-array> + <string-array name="mock_autocomplete_sign_in_suggestions"> + <item>user-1</item> + <item>user-2</item> + </string-array> + </resources> |