Commit 501ad812 authored by Lukasz Suder's avatar Lukasz Suder Committed by Commit Bot

[Autofill Assistant] Instantiates AA only if it's turned on.

Initial screen is shown without the initialization of UIDelegate,
that allows postoponing the init of native side.

Bug: 806868
Change-Id: I047ec5bf12d454ce7a51b31ad68c758baf10869b
Reviewed-on: https://chromium-review.googlesource.com/c/1352163
Commit-Queue: Lukasz Suder <lsuder@chromium.org>
Reviewed-by: default avatarGanggui Tang <gogerald@chromium.org>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612135}
parent 31885f53
......@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.autofill_assistant;
import android.os.Bundle;
import android.support.annotation.Nullable;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
......@@ -27,24 +28,43 @@ public class AutofillAssistantFacade {
private static final String PARAMETER_ENABLED = "ENABLED";
/** Returns true if all conditions are satisfied to start Autofill Assistant. */
public static boolean isConfigured(Bundle intentExtras) {
public static boolean isConfigured(@Nullable Bundle intentExtras) {
return getBooleanParameter(intentExtras, PARAMETER_ENABLED)
&& !AutofillAssistantStudy.getUrl().isEmpty()
&& AutofillAssistantPreferencesUtil.isAutofillAssistantSwitchOn();
&& AutofillAssistantPreferencesUtil.canShowAutofillAssistant();
}
/** Starts Autofill Assistant on the given {@code activity}. */
public static void start(ChromeActivity activity) {
Map<String, String> parameters = extractParameters(activity.getInitialIntent().getExtras());
parameters.remove(PARAMETER_ENABLED);
if (!AutofillAssistantPreferencesUtil.getSkipInitScreenPreference()) {
FirstRunScreen.show(activity, (result) -> {
if (result) initiateAutofillAssistant(activity, parameters);
});
return;
}
if (AutofillAssistantPreferencesUtil.isAutofillAssistantSwitchOn()
&& AutofillAssistantPreferencesUtil.getSkipInitScreenPreference()) {
initiateAutofillAssistant(activity, parameters);
}
// We don't have consent to start Autofill Assistant and cannot show initial screen.
// Do nothing.
}
/**
* Instantiates all essential Autofill Assistant components and starts it.
*/
private static void initiateAutofillAssistant(
ChromeActivity activity, Map<String, String> parameters) {
AutofillAssistantUiController controller =
new AutofillAssistantUiController(activity, parameters);
UiDelegateHolder delegateHolder = new UiDelegateHolder(
controller, new AutofillAssistantUiDelegate(activity, controller));
initTabObservers(activity, delegateHolder);
controller.start(delegateHolder, Details.makeFromParameters(parameters));
controller.init(delegateHolder, Details.makeFromParameters(parameters));
}
private static void initTabObservers(ChromeActivity activity, UiDelegateHolder delegateHolder) {
......@@ -74,16 +94,19 @@ public class AutofillAssistantFacade {
}
/** Return the value if the given boolean parameter from the extras. */
private static boolean getBooleanParameter(Bundle extras, String parameterName) {
return extras.getBoolean(INTENT_EXTRA_PREFIX + parameterName, false);
private static boolean getBooleanParameter(@Nullable Bundle extras, String parameterName) {
return extras != null && extras.getBoolean(INTENT_EXTRA_PREFIX + parameterName, false);
}
/** Returns a map containing the extras starting with {@link #INTENT_EXTRA_PREFIX}. */
private static Map<String, String> extractParameters(Bundle extras) {
private static Map<String, String> extractParameters(@Nullable Bundle extras) {
Map<String, String> result = new HashMap<>();
for (String key : extras.keySet()) {
if (key.startsWith(INTENT_EXTRA_PREFIX)) {
result.put(key.substring(INTENT_EXTRA_PREFIX.length()), extras.get(key).toString());
if (extras != null) {
for (String key : extras.keySet()) {
if (key.startsWith(INTENT_EXTRA_PREFIX)) {
result.put(key.substring(INTENT_EXTRA_PREFIX.length()),
extras.get(key).toString());
}
}
}
return result;
......
......@@ -8,7 +8,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.chrome.browser.preferences.autofill_assistant.AutofillAssistantPreferences;
/** Autofill Assistant related preferences util class. */
public class AutofillAssistantPreferencesUtil {
class AutofillAssistantPreferencesUtil {
// Avoid instatiation by accident.
private AutofillAssistantPreferencesUtil() {}
......@@ -19,24 +19,33 @@ public class AutofillAssistantPreferencesUtil {
"AUTOFILL_ASSISTANT_SKIP_INIT_SCREEN";
/** Checks whether the Autofill Assistant switch preference in settings is on. */
public static boolean isAutofillAssistantSwitchOn() {
static boolean isAutofillAssistantSwitchOn() {
return ContextUtils.getAppSharedPreferences().getBoolean(
AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH, true);
}
/** Gets whether skip initial screen preference. */
public static boolean getSkipInitScreenPreference() {
static boolean getSkipInitScreenPreference() {
return ContextUtils.getAppSharedPreferences().getBoolean(
AUTOFILL_ASSISTANT_SKIP_INIT_SCREEN, false);
}
/**
* Returns true if the switch for AutofillAssistant is turned on or the init screen can
* be shown. The later is important if the switched is turned off, but we can ask again
* to enable AutofillAssistant.
*/
static boolean canShowAutofillAssistant() {
return isAutofillAssistantSwitchOn() || !getSkipInitScreenPreference();
}
/**
* Sets preferences from the initial screen.
*
* @param accept Flag indicates whether this service is accepted.
* @param dontShowAgain Flag indicates whether initial screen should be shown again next time.
*/
public static void setInitialPreferences(boolean accept, boolean dontShowAgain) {
static void setInitialPreferences(boolean accept, boolean dontShowAgain) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH, accept)
......
......@@ -89,17 +89,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
parameters.get(PARAMETER_USER_EMAIL), activity.getInitialIntent().getExtras());
}
void start(UiDelegateHolder uiDelegateHolder, Details details) {
mUiDelegateHolder = uiDelegateHolder;
// Do not show details until 'onInitOk'.
mCurrentDetails = details;
mUiDelegateHolder.startOrSkipInitScreen();
}
@Override
public void onInitOk() {
assert mUiDelegateHolder != null;
mUiDelegateHolder.performUiOperation(uiDelegate -> uiDelegate.showDetails(mCurrentDetails));
public void init(UiDelegateHolder delegateHolder, Details details) {
mUiDelegateHolder = delegateHolder;
maybeUpdateDetails(details);
nativeStart(mUiControllerAndroid, mInitialUrl);
}
......@@ -108,11 +100,6 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
mUiDelegateHolder.dismiss(R.string.autofill_assistant_stopped);
}
@Override
public void onInitRejected() {
mUiDelegateHolder.shutdown();
}
@Override
public Details getDetails() {
return mCurrentDetails;
......
......@@ -30,7 +30,6 @@ import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
......@@ -120,12 +119,6 @@ class AutofillAssistantUiDelegate {
*/
void onDismiss();
/**
* Called when the user chose not to use the assistant from the
* onboarding screen.
*/
void onInitRejected();
/**
* Called when a script has been selected.
*
......@@ -174,11 +167,6 @@ class AutofillAssistantUiDelegate {
* @return A string describing the current execution context.
*/
String getDebugContext();
/**
* Called when the init was successful.
*/
void onInitOk();
}
/** Describes a chip to display. */
......@@ -762,47 +750,6 @@ class AutofillAssistantUiDelegate {
setCarouselChildViews(childViews, /* alignRight= */ false);
}
/**
* Starts the init screen unless it has been marked to be skipped.
*/
public void startOrSkipInitScreen() {
if (AutofillAssistantPreferencesUtil.getSkipInitScreenPreference()) {
mClient.onInitOk();
return;
}
showInitScreen();
}
/**
* Shows the init screen and launch the autofill assistant when it succeeds.
*/
public void showInitScreen() {
View initView = LayoutInflater.from(mActivity)
.inflate(R.layout.init_screen, mCoordinatorView)
.findViewById(R.id.init_screen);
// Set focusable for accessibility.
initView.findViewById(R.id.init).setFocusable(true);
// Set default state to checked.
((CheckBox) initView.findViewById(R.id.checkbox_dont_show_init_again)).setChecked(true);
initView.findViewById(R.id.button_init_ok)
.setOnClickListener(unusedView -> onInitClicked(true, initView));
initView.findViewById(R.id.button_init_not_ok)
.setOnClickListener(unusedView -> onInitClicked(false, initView));
initView.announceForAccessibility(
mActivity.getString(R.string.autofill_assistant_first_run_accessibility));
}
private void onInitClicked(boolean accept, View initView) {
CheckBox checkBox = initView.findViewById(R.id.checkbox_dont_show_init_again);
AutofillAssistantPreferencesUtil.setInitialPreferences(accept, checkBox.isChecked());
mCoordinatorView.removeView(initView);
if (accept)
mClient.onInitOk();
else
mClient.onInitRejected();
}
/**
* Show the payment request UI.
*
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.autofill_assistant;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import org.chromium.base.Callback;
import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.ChromeActivity;
/** Class for managing the first run screen. */
class FirstRunScreen {
/**
* Shows the first run screen and calls callback with the result.
*/
static void show(ChromeActivity activity, Callback<Boolean> callback) {
ViewGroup coordinatorView = (ViewGroup) activity.findViewById(
org.chromium.chrome.autofill_assistant.R.id.coordinator);
View initView = LayoutInflater.from(activity)
.inflate(R.layout.init_screen, coordinatorView)
.findViewById(R.id.init_screen);
// Set focusable for accessibility.
initView.findViewById(R.id.init).setFocusable(true);
initView.findViewById(R.id.button_init_ok)
.setOnClickListener(unusedView -> onClicked(true, initView, activity, callback));
initView.findViewById(R.id.button_init_not_ok)
.setOnClickListener(unusedView -> onClicked(false, initView, activity, callback));
initView.announceForAccessibility(
activity.getString(R.string.autofill_assistant_first_run_accessibility));
}
private static void onClicked(
boolean accept, View initView, ChromeActivity activity, Callback<Boolean> callback) {
ViewGroup coordinatorView = (ViewGroup) activity.findViewById(
org.chromium.chrome.autofill_assistant.R.id.coordinator);
CheckBox checkBox = initView.findViewById(
org.chromium.chrome.autofill_assistant.R.id.checkbox_dont_show_init_again);
AutofillAssistantPreferencesUtil.setInitialPreferences(accept, checkBox.isChecked());
coordinatorView.removeView(initView);
callback.onResult(accept);
}
}
......@@ -35,13 +35,6 @@ class UiDelegateHolder {
mUiDelegate = uiDelegate;
}
/**
* Starts the init screen unless it has been marked to be skipped.
*/
public void startOrSkipInitScreen() {
mUiDelegate.startOrSkipInitScreen();
}
/**
* Perform a UI operation:
* - directly if we are not in a pause state.
......
......@@ -134,6 +134,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiDelegate.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/Details.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/FeedbackContext.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/FirstRunScreen.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/UiDelegateHolder.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/ui/BottomBarAnimations.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/ui/PaymentRequestBottomBar.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment