Commit 7f817a42 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Moved events and interactions into dedicated files.

This is a refactoring only and should have no user-facing changes.

Bug: b/145043394
Change-Id: If820f681fefd486b7f0700800c610152339293d4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2120353Reviewed-by: default avatarMarian Fechete <marianfe@google.com>
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#753197}
parent f0a585c5
...@@ -120,6 +120,7 @@ android_library("java") { ...@@ -120,6 +120,7 @@ android_library("java") {
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiModel.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiModel.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiViewBinder.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiViewBinder.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantValue.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantValue.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewEvents.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewFactory.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewFactory.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewInteractions.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewInteractions.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/header/AnimatedProgressBar.java", "java/src/org/chromium/chrome/browser/autofill_assistant/header/AnimatedProgressBar.java",
...@@ -185,6 +186,7 @@ generate_jni("jni_headers") { ...@@ -185,6 +186,7 @@ generate_jni("jni_headers") {
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiDelegate.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiDelegate.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiModel.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantGenericUiModel.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantValue.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantValue.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewEvents.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewFactory.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewFactory.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewInteractions.java", "java/src/org/chromium/chrome/browser/autofill_assistant/generic_ui/AssistantViewInteractions.java",
"java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderDelegate.java", "java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderDelegate.java",
......
// Copyright 2020 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.generic_ui;
import android.view.View;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
/** JNI bridge between {@code generic_ui_events_android} and Java. */
@JNINamespace("autofill_assistant")
public class AssistantViewEvents {
@CalledByNative
private static void setOnClickListener(
View view, String identifier, AssistantGenericUiDelegate delegate) {
view.setOnClickListener(unused -> delegate.onViewClicked(identifier));
}
}
...@@ -26,15 +26,9 @@ import java.util.ArrayList; ...@@ -26,15 +26,9 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/** JNI bridge between {@code interaction_handler_android} and Java. */ /** JNI bridge between {@code generic_ui_interactions_android} and Java. */
@JNINamespace("autofill_assistant") @JNINamespace("autofill_assistant")
public class AssistantViewInteractions { public class AssistantViewInteractions {
@CalledByNative
private static void setOnClickListener(
View view, String identifier, AssistantGenericUiDelegate delegate) {
view.setOnClickListener(unused -> delegate.onViewClicked(identifier));
}
@CalledByNative @CalledByNative
private static void showListPopup(Context context, String[] itemNames, private static void showListPopup(Context context, String[] itemNames,
@PopupItemType int[] itemTypes, int[] selectedItems, boolean multiple, @PopupItemType int[] itemTypes, int[] selectedItems, boolean multiple,
......
...@@ -2332,6 +2332,10 @@ jumbo_static_library("browser") { ...@@ -2332,6 +2332,10 @@ jumbo_static_library("browser") {
"android/autofill_assistant/client_android.h", "android/autofill_assistant/client_android.h",
"android/autofill_assistant/generic_ui_controller_android.cc", "android/autofill_assistant/generic_ui_controller_android.cc",
"android/autofill_assistant/generic_ui_controller_android.h", "android/autofill_assistant/generic_ui_controller_android.h",
"android/autofill_assistant/generic_ui_events_android.cc",
"android/autofill_assistant/generic_ui_events_android.h",
"android/autofill_assistant/generic_ui_interactions_android.cc",
"android/autofill_assistant/generic_ui_interactions_android.h",
"android/autofill_assistant/interaction_handler_android.cc", "android/autofill_assistant/interaction_handler_android.cc",
"android/autofill_assistant/interaction_handler_android.h", "android/autofill_assistant/interaction_handler_android.h",
"android/autofill_assistant/ui_controller_android.cc", "android/autofill_assistant/ui_controller_android.cc",
......
// Copyright 2020 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.
#include "chrome/browser/android/autofill_assistant/generic_ui_events_android.h"
#include "base/android/jni_string.h"
#include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewEvents_jni.h"
namespace autofill_assistant {
namespace android_events {
void SetOnClickListener(JNIEnv* env,
base::android::ScopedJavaGlobalRef<jobject> jview,
base::android::ScopedJavaGlobalRef<jobject> jdelegate,
const OnViewClickedEventProto& proto) {
Java_AssistantViewEvents_setOnClickListener(
env, jview,
base::android::ConvertUTF8ToJavaString(env, proto.view_identifier()),
jdelegate);
}
} // namespace android_events
} // namespace autofill_assistant
// Copyright 2020 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.
#ifndef CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_EVENTS_ANDROID_H_
#define CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_EVENTS_ANDROID_H_
#include <string>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "components/autofill_assistant/browser/interactions.pb.h"
namespace autofill_assistant {
namespace android_events {
// Sets a click listener for |jview|.
void SetOnClickListener(JNIEnv* env,
base::android::ScopedJavaGlobalRef<jobject> jview,
base::android::ScopedJavaGlobalRef<jobject> jdelegate,
const OnViewClickedEventProto& proto);
} // namespace android_events
} // namespace autofill_assistant
#endif // CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_EVENTS_ANDROID_H_
// Copyright 2020 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.
#include "chrome/browser/android/autofill_assistant/generic_ui_interactions_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/optional.h"
#include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewInteractions_jni.h"
#include "chrome/browser/android/autofill_assistant/ui_controller_android_utils.h"
#include "components/autofill_assistant/browser/user_model.h"
namespace autofill_assistant {
namespace android_interactions {
void SetValue(base::WeakPtr<BasicInteractions> basic_interactions,
const SetModelValueProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->SetValue(proto);
}
void ComputeValue(base::WeakPtr<BasicInteractions> basic_interactions,
const ComputeValueProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->ComputeValue(proto);
}
void SetUserActions(base::WeakPtr<BasicInteractions> basic_interactions,
const SetUserActionsProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->SetUserActions(proto);
}
void EndAction(base::WeakPtr<BasicInteractions> basic_interactions,
const EndActionProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->EndAction(proto);
}
void ToggleUserAction(base::WeakPtr<BasicInteractions> basic_interactions,
const ToggleUserActionProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->ToggleUserAction(proto);
}
void ShowInfoPopup(const InfoPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext) {
JNIEnv* env = base::android::AttachCurrentThread();
auto jcontext_local = base::android::ScopedJavaLocalRef<jobject>(jcontext);
ui_controller_android_utils::ShowJavaInfoPopup(
env, ui_controller_android_utils::CreateJavaInfoPopup(env, proto),
jcontext_local);
}
void ShowListPopup(base::WeakPtr<UserModel> user_model,
const ShowListPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
if (!user_model) {
return;
}
auto item_names = user_model->GetValue(proto.item_names_model_identifier());
if (!item_names.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.item_names_model_identifier() << "' not found in model.";
return;
}
if (item_names->strings().values().size() == 0) {
DVLOG(2) << "Failed to show list popup: the list of item names in '"
<< proto.item_names_model_identifier() << "' was empty.";
return;
}
base::Optional<ValueProto> item_types;
if (proto.has_item_types_model_identifier()) {
item_types = user_model->GetValue(proto.item_types_model_identifier());
if (!item_types.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.item_types_model_identifier()
<< "' not found in the model.";
return;
}
if (item_types->ints().values().size() !=
item_names->strings().values().size()) {
DVLOG(2) << "Failed to show list popup: Expected item_types to contain "
<< item_names->strings().values().size() << " integers, but got "
<< item_types->ints().values().size();
return;
}
} else {
item_types = ValueProto();
for (int i = 0; i < item_names->strings().values().size(); ++i) {
item_types->mutable_ints()->add_values(
static_cast<int>(ShowListPopupProto::ENABLED));
}
}
auto selected_indices =
user_model->GetValue(proto.selected_item_indices_model_identifier());
if (!selected_indices.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.selected_item_indices_model_identifier()
<< "' not found in model.";
return;
}
if (!(*selected_indices == ValueProto()) &&
selected_indices->kind_case() != ValueProto::kInts) {
DVLOG(2) << "Failed to show list popup: expected '"
<< proto.selected_item_indices_model_identifier()
<< "' to be int[], but was of type "
<< selected_indices->kind_case();
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<std::string> item_names_vec;
std::copy(item_names->strings().values().begin(),
item_names->strings().values().end(),
std::back_inserter(item_names_vec));
std::vector<int> item_types_vec;
std::copy(item_types->ints().values().begin(),
item_types->ints().values().end(),
std::back_inserter(item_types_vec));
std::vector<int> selected_indices_vec;
std::copy(selected_indices->ints().values().begin(),
selected_indices->ints().values().end(),
std::back_inserter(selected_indices_vec));
Java_AssistantViewInteractions_showListPopup(
env, jcontext, base::android::ToJavaArrayOfStrings(env, item_names_vec),
base::android::ToJavaIntArray(env, item_types_vec),
base::android::ToJavaIntArray(env, selected_indices_vec),
proto.allow_multiselect(),
base::android::ConvertUTF8ToJavaString(
env, proto.selected_item_indices_model_identifier()),
proto.selected_item_names_model_identifier().empty()
? nullptr
: base::android::ConvertUTF8ToJavaString(
env, proto.selected_item_names_model_identifier()),
jdelegate);
}
void ShowCalendarPopup(base::WeakPtr<UserModel> user_model,
const ShowCalendarPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
if (!user_model) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
auto initial_date = user_model->GetValue(proto.date_model_identifier());
if (!initial_date.has_value()) {
DVLOG(2) << "Failed to show calendar popup: "
<< proto.date_model_identifier() << " not found in model";
return;
}
if (*initial_date != ValueProto() &&
initial_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: date_model_identifier must be "
"empty or contain single date, but was "
<< *initial_date;
return;
}
auto min_date = user_model->GetValue(proto.min_date_model_identifier());
if (!min_date.has_value() || min_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: min_date not found or invalid "
"in user model at "
<< proto.min_date_model_identifier();
return;
}
auto max_date = user_model->GetValue(proto.max_date_model_identifier());
if (!max_date.has_value() || max_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: max_date not found or invalid "
"in user model at "
<< proto.max_date_model_identifier();
return;
}
jboolean jsuccess = Java_AssistantViewInteractions_showCalendarPopup(
env, jcontext,
*initial_date != ValueProto()
? ui_controller_android_utils::ToJavaValue(env, *initial_date)
: nullptr,
ui_controller_android_utils::ToJavaValue(env, *min_date),
ui_controller_android_utils::ToJavaValue(env, *max_date),
base::android::ConvertUTF8ToJavaString(env,
proto.date_model_identifier()),
jdelegate);
if (!jsuccess) {
DVLOG(2) << "Failed to show calendar popup: JNI call failed";
}
}
void SetViewText(
base::WeakPtr<UserModel> user_model,
const SetTextProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) {
return;
}
auto text = user_model->GetValue(proto.model_identifier());
if (!text.has_value()) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": "
<< proto.model_identifier() << " not found in model";
return;
}
if (text->strings().values_size() != 1) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier()
<< ": expected " << proto.model_identifier()
<< " to contain single string, but was instead " << *text;
return;
}
auto jview = views->find(proto.view_identifier());
if (jview == views->end()) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": "
<< " view not found";
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewText(
env, jview->second,
base::android::ConvertUTF8ToJavaString(env, text->strings().values(0)));
}
void SetViewVisibility(
base::WeakPtr<UserModel> user_model,
const SetViewVisibilityProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) {
return;
}
auto jview = views->find(proto.view_identifier());
if (jview == views->end()) {
DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier()
<< ": view not found";
return;
}
auto visible_value = user_model->GetValue(proto.model_identifier());
if (!visible_value.has_value() ||
visible_value->booleans().values_size() != 1) {
DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier()
<< ": " << proto.model_identifier()
<< " did not contain single boolean";
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewVisibility(
env, jview->second,
ui_controller_android_utils::ToJavaValue(env, *visible_value));
}
} // namespace android_interactions
} // namespace autofill_assistant
// Copyright 2020 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.
#ifndef CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_INTERACTIONS_ANDROID_H_
#define CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_INTERACTIONS_ANDROID_H_
#include <string>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/memory/weak_ptr.h"
#include "components/autofill_assistant/browser/basic_interactions.h"
#include "components/autofill_assistant/browser/interactions.pb.h"
namespace autofill_assistant {
namespace android_interactions {
// Writes a value to the model.
void SetValue(base::WeakPtr<BasicInteractions> basic_interactions,
const SetModelValueProto& proto);
// Computes a value and writes it to the model.
void ComputeValue(base::WeakPtr<BasicInteractions> basic_interactions,
const ComputeValueProto& proto);
// Sets the list of available user actions (i.e., chips and direct actions).
void SetUserActions(base::WeakPtr<BasicInteractions> basic_interactions,
const SetUserActionsProto& proto);
// Ends the current ShowGenericUi action.
void EndAction(base::WeakPtr<BasicInteractions> basic_interactions,
const EndActionProto& proto);
// Enables or disables a particular user action.
void ToggleUserAction(base::WeakPtr<BasicInteractions> basic_interactions,
const ToggleUserActionProto& proto);
// Displays an info popup on the screen.
void ShowInfoPopup(const InfoPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext);
// Displays a list popup on the screen.
void ShowListPopup(base::WeakPtr<UserModel> user_model,
const ShowListPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate);
// Displays a calendar popup on the screen.
void ShowCalendarPopup(base::WeakPtr<UserModel> user_model,
const ShowCalendarPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate);
// Sets the text of a view.
void SetViewText(
base::WeakPtr<UserModel> user_model,
const SetTextProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// Sets the visibility of a view.
void SetViewVisibility(
base::WeakPtr<UserModel> user_model,
const SetViewVisibilityProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
} // namespace android_interactions
} // namespace autofill_assistant
#endif // CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_GENERIC_UI_INTERACTIONS_ANDROID_H_
...@@ -5,14 +5,13 @@ ...@@ -5,14 +5,13 @@
#include "chrome/browser/android/autofill_assistant/interaction_handler_android.h" #include "chrome/browser/android/autofill_assistant/interaction_handler_android.h"
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include "base/android/jni_array.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewInteractions_jni.h"
#include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h" #include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h"
#include "chrome/browser/android/autofill_assistant/ui_controller_android_utils.h" #include "chrome/browser/android/autofill_assistant/generic_ui_events_android.h"
#include "chrome/browser/android/autofill_assistant/generic_ui_interactions_android.h"
#include "components/autofill_assistant/browser/basic_interactions.h" #include "components/autofill_assistant/browser/basic_interactions.h"
#include "components/autofill_assistant/browser/interactions.pb.h" #include "components/autofill_assistant/browser/interactions.pb.h"
#include "components/autofill_assistant/browser/ui_delegate.h" #include "components/autofill_assistant/browser/ui_delegate.h"
...@@ -23,50 +22,8 @@ namespace autofill_assistant { ...@@ -23,50 +22,8 @@ namespace autofill_assistant {
namespace { namespace {
// Note regarding Try* methods: these are simple wrappers around basic // A simple wrapper around a basic interaction, needed because we can't directly
// interactions. They are needed because it is impossible to directly bind to // bind a repeating callback to a method with non-void return value.
// non-void return methods with a weak ptr.
void TrySetValue(base::WeakPtr<BasicInteractions> basic_interactions,
const SetModelValueProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->SetValue(proto);
}
void TryComputeValue(base::WeakPtr<BasicInteractions> basic_interactions,
const ComputeValueProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->ComputeValue(proto);
}
void SetUserActionsIgnoreReturn(
base::WeakPtr<BasicInteractions> basic_interactions,
const SetUserActionsProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->SetUserActions(proto);
}
void TryEndAction(base::WeakPtr<BasicInteractions> basic_interactions,
const EndActionProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->EndAction(proto);
}
void TryToggleUserAction(base::WeakPtr<BasicInteractions> basic_interactions,
const ToggleUserActionProto& proto) {
if (!basic_interactions) {
return;
}
basic_interactions->ToggleUserAction(proto);
}
void TryRunConditionalCallback( void TryRunConditionalCallback(
base::WeakPtr<BasicInteractions> basic_interactions, base::WeakPtr<BasicInteractions> basic_interactions,
const std::string& condition_identifier, const std::string& condition_identifier,
...@@ -77,224 +34,6 @@ void TryRunConditionalCallback( ...@@ -77,224 +34,6 @@ void TryRunConditionalCallback(
basic_interactions->RunConditionalCallback(condition_identifier, callback); basic_interactions->RunConditionalCallback(condition_identifier, callback);
} }
void ShowInfoPopup(const InfoPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext) {
JNIEnv* env = base::android::AttachCurrentThread();
auto jcontext_local = base::android::ScopedJavaLocalRef<jobject>(jcontext);
ui_controller_android_utils::ShowJavaInfoPopup(
env, ui_controller_android_utils::CreateJavaInfoPopup(env, proto),
jcontext_local);
}
void ShowListPopup(base::WeakPtr<UserModel> user_model,
const ShowListPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
if (!user_model) {
return;
}
auto item_names = user_model->GetValue(proto.item_names_model_identifier());
if (!item_names.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.item_names_model_identifier() << "' not found in model.";
return;
}
if (item_names->strings().values().size() == 0) {
DVLOG(2) << "Failed to show list popup: the list of item names in '"
<< proto.item_names_model_identifier() << "' was empty.";
return;
}
base::Optional<ValueProto> item_types;
if (proto.has_item_types_model_identifier()) {
item_types = user_model->GetValue(proto.item_types_model_identifier());
if (!item_types.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.item_types_model_identifier()
<< "' not found in the model.";
return;
}
if (item_types->ints().values().size() !=
item_names->strings().values().size()) {
DVLOG(2) << "Failed to show list popup: Expected item_types to contain "
<< item_names->strings().values().size() << " integers, but got "
<< item_types->ints().values().size();
return;
}
} else {
item_types = ValueProto();
for (int i = 0; i < item_names->strings().values().size(); ++i) {
item_types->mutable_ints()->add_values(
static_cast<int>(ShowListPopupProto::ENABLED));
}
}
auto selected_indices =
user_model->GetValue(proto.selected_item_indices_model_identifier());
if (!selected_indices.has_value()) {
DVLOG(2) << "Failed to show list popup: '"
<< proto.selected_item_indices_model_identifier()
<< "' not found in model.";
return;
}
if (!(*selected_indices == ValueProto()) &&
selected_indices->kind_case() != ValueProto::kInts) {
DVLOG(2) << "Failed to show list popup: expected '"
<< proto.selected_item_indices_model_identifier()
<< "' to be int[], but was of type "
<< selected_indices->kind_case();
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<std::string> item_names_vec;
std::copy(item_names->strings().values().begin(),
item_names->strings().values().end(),
std::back_inserter(item_names_vec));
std::vector<int> item_types_vec;
std::copy(item_types->ints().values().begin(),
item_types->ints().values().end(),
std::back_inserter(item_types_vec));
std::vector<int> selected_indices_vec;
std::copy(selected_indices->ints().values().begin(),
selected_indices->ints().values().end(),
std::back_inserter(selected_indices_vec));
Java_AssistantViewInteractions_showListPopup(
env, jcontext, base::android::ToJavaArrayOfStrings(env, item_names_vec),
base::android::ToJavaIntArray(env, item_types_vec),
base::android::ToJavaIntArray(env, selected_indices_vec),
proto.allow_multiselect(),
base::android::ConvertUTF8ToJavaString(
env, proto.selected_item_indices_model_identifier()),
proto.selected_item_names_model_identifier().empty()
? nullptr
: base::android::ConvertUTF8ToJavaString(
env, proto.selected_item_names_model_identifier()),
jdelegate);
}
void ShowCalendarPopup(base::WeakPtr<UserModel> user_model,
const ShowCalendarPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
if (!user_model) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
auto initial_date = user_model->GetValue(proto.date_model_identifier());
if (!initial_date.has_value()) {
DVLOG(2) << "Failed to show calendar popup: "
<< proto.date_model_identifier() << " not found in model";
return;
}
if (*initial_date != ValueProto() &&
initial_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: date_model_identifier must be "
"empty or contain single date, but was "
<< *initial_date;
return;
}
auto min_date = user_model->GetValue(proto.min_date_model_identifier());
if (!min_date.has_value() || min_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: min_date not found or invalid "
"in user model at "
<< proto.min_date_model_identifier();
return;
}
auto max_date = user_model->GetValue(proto.max_date_model_identifier());
if (!max_date.has_value() || max_date->dates().values().size() != 1) {
DVLOG(2) << "Failed to show calendar popup: max_date not found or invalid "
"in user model at "
<< proto.max_date_model_identifier();
return;
}
jboolean jsuccess = Java_AssistantViewInteractions_showCalendarPopup(
env, jcontext,
*initial_date != ValueProto()
? ui_controller_android_utils::ToJavaValue(env, *initial_date)
: nullptr,
ui_controller_android_utils::ToJavaValue(env, *min_date),
ui_controller_android_utils::ToJavaValue(env, *max_date),
base::android::ConvertUTF8ToJavaString(env,
proto.date_model_identifier()),
jdelegate);
if (!jsuccess) {
DVLOG(2) << "Failed to show calendar popup: JNI call failed";
}
}
void SetViewText(
base::WeakPtr<UserModel> user_model,
const SetTextProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) {
return;
}
auto text = user_model->GetValue(proto.model_identifier());
if (!text.has_value()) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": "
<< proto.model_identifier() << " not found in model";
return;
}
if (text->strings().values_size() != 1) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier()
<< ": expected " << proto.model_identifier()
<< " to contain single string, but was instead " << *text;
return;
}
auto jview = views->find(proto.view_identifier());
if (jview == views->end()) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": "
<< " view not found";
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewText(
env, jview->second,
base::android::ConvertUTF8ToJavaString(env, text->strings().values(0)));
}
void SetViewVisibility(
base::WeakPtr<UserModel> user_model,
const SetViewVisibilityProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) {
return;
}
auto jview = views->find(proto.view_identifier());
if (jview == views->end()) {
DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier()
<< ": view not found";
return;
}
auto visible_value = user_model->GetValue(proto.model_identifier());
if (!visible_value.has_value() ||
visible_value->booleans().values_size() != 1) {
DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier()
<< ": " << proto.model_identifier()
<< " did not contain single boolean";
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewVisibility(
env, jview->second,
ui_controller_android_utils::ToJavaValue(env, *visible_value));
}
base::Optional<EventHandler::EventKey> CreateEventKeyFromProto( base::Optional<EventHandler::EventKey> CreateEventKeyFromProto(
const EventProto& proto, const EventProto& proto,
JNIEnv* env, JNIEnv* env,
...@@ -311,11 +50,8 @@ base::Optional<EventHandler::EventKey> CreateEventKeyFromProto( ...@@ -311,11 +50,8 @@ base::Optional<EventHandler::EventKey> CreateEventKeyFromProto(
<< proto.on_view_clicked().view_identifier() << "' found"; << proto.on_view_clicked().view_identifier() << "' found";
return base::nullopt; return base::nullopt;
} }
Java_AssistantViewInteractions_setOnClickListener( android_events::SetOnClickListener(env, jview->second, jdelegate,
env, jview->second, proto.on_view_clicked());
base::android::ConvertUTF8ToJavaString(
env, proto.on_view_clicked().view_identifier()),
jdelegate);
return base::Optional<EventHandler::EventKey>( return base::Optional<EventHandler::EventKey>(
{proto.kind_case(), proto.on_view_clicked().view_identifier()}); {proto.kind_case(), proto.on_view_clicked().view_identifier()});
} }
...@@ -346,11 +82,12 @@ CreateInteractionCallbackFromProto( ...@@ -346,11 +82,12 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&TrySetValue, basic_interactions->GetWeakPtr(), base::BindRepeating(&android_interactions::SetValue,
basic_interactions->GetWeakPtr(),
proto.set_value())); proto.set_value()));
case CallbackProto::kShowInfoPopup: { case CallbackProto::kShowInfoPopup: {
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&ShowInfoPopup, base::BindRepeating(&android_interactions::ShowInfoPopup,
proto.show_info_popup().info_popup(), jcontext)); proto.show_info_popup().info_popup(), jcontext));
} }
case CallbackProto::kShowListPopup: case CallbackProto::kShowListPopup:
...@@ -367,8 +104,9 @@ CreateInteractionCallbackFromProto( ...@@ -367,8 +104,9 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&ShowListPopup, user_model->GetWeakPtr(), base::BindRepeating(&android_interactions::ShowListPopup,
proto.show_list_popup(), jcontext, jdelegate)); user_model->GetWeakPtr(), proto.show_list_popup(),
jcontext, jdelegate));
case CallbackProto::kComputeValue: case CallbackProto::kComputeValue:
if (proto.compute_value().result_model_identifier().empty()) { if (proto.compute_value().result_model_identifier().empty()) {
VLOG(1) << "Error creating ComputeValue interaction: " VLOG(1) << "Error creating ComputeValue interaction: "
...@@ -376,7 +114,7 @@ CreateInteractionCallbackFromProto( ...@@ -376,7 +114,7 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&TryComputeValue, base::BindRepeating(&android_interactions::ComputeValue,
basic_interactions->GetWeakPtr(), basic_interactions->GetWeakPtr(),
proto.compute_value())); proto.compute_value()));
case CallbackProto::kSetUserActions: case CallbackProto::kSetUserActions:
...@@ -386,12 +124,13 @@ CreateInteractionCallbackFromProto( ...@@ -386,12 +124,13 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&SetUserActionsIgnoreReturn, base::BindRepeating(&android_interactions::SetUserActions,
basic_interactions->GetWeakPtr(), basic_interactions->GetWeakPtr(),
proto.set_user_actions())); proto.set_user_actions()));
case CallbackProto::kEndAction: case CallbackProto::kEndAction:
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&TryEndAction, basic_interactions->GetWeakPtr(), base::BindRepeating(&android_interactions::EndAction,
basic_interactions->GetWeakPtr(),
proto.end_action())); proto.end_action()));
case CallbackProto::kShowCalendarPopup: case CallbackProto::kShowCalendarPopup:
if (proto.show_calendar_popup().date_model_identifier().empty()) { if (proto.show_calendar_popup().date_model_identifier().empty()) {
...@@ -400,7 +139,8 @@ CreateInteractionCallbackFromProto( ...@@ -400,7 +139,8 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&ShowCalendarPopup, user_model->GetWeakPtr(), base::BindRepeating(&android_interactions::ShowCalendarPopup,
user_model->GetWeakPtr(),
proto.show_calendar_popup(), jcontext, proto.show_calendar_popup(), jcontext,
jdelegate)); jdelegate));
case CallbackProto::kSetText: case CallbackProto::kSetText:
...@@ -415,8 +155,9 @@ CreateInteractionCallbackFromProto( ...@@ -415,8 +155,9 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&SetViewText, user_model->GetWeakPtr(), base::BindRepeating(&android_interactions::SetViewText,
proto.set_text(), views)); user_model->GetWeakPtr(), proto.set_text(),
views));
case CallbackProto::kToggleUserAction: case CallbackProto::kToggleUserAction:
if (proto.toggle_user_action().user_actions_model_identifier().empty()) { if (proto.toggle_user_action().user_actions_model_identifier().empty()) {
VLOG(1) << "Error creating ToggleUserAction interaction: " VLOG(1) << "Error creating ToggleUserAction interaction: "
...@@ -434,7 +175,7 @@ CreateInteractionCallbackFromProto( ...@@ -434,7 +175,7 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&TryToggleUserAction, base::BindRepeating(&android_interactions::ToggleUserAction,
basic_interactions->GetWeakPtr(), basic_interactions->GetWeakPtr(),
proto.toggle_user_action())); proto.toggle_user_action()));
case CallbackProto::kSetViewVisibility: case CallbackProto::kSetViewVisibility:
...@@ -449,7 +190,8 @@ CreateInteractionCallbackFromProto( ...@@ -449,7 +190,8 @@ CreateInteractionCallbackFromProto(
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionHandlerAndroid::InteractionCallback>( return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&SetViewVisibility, user_model->GetWeakPtr(), base::BindRepeating(&android_interactions::SetViewVisibility,
user_model->GetWeakPtr(),
proto.set_view_visibility(), views)); proto.set_view_visibility(), views));
case CallbackProto::KIND_NOT_SET: case CallbackProto::KIND_NOT_SET:
VLOG(1) << "Error creating interaction: kind not set"; VLOG(1) << "Error creating interaction: kind not set";
......
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