Commit bd9703f6 authored by Yue Li's avatar Yue Li Committed by Commit Bot

Merge Assistant preferences logic into AssistantState

Merge the current Assistant prefs controller logic into the
AssistantStateBase. Will also merge the logic of
VoiceInteractionController into this controller in the next change.

Bug: b/110211045
Test: Run existing tests
Change-Id: I1d3a8388fd66217baad44d7c7043ffd75830e42d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1727733
Commit-Queue: Yue Li <updowndota@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#684455}
parent c8c12cbb
...@@ -238,8 +238,10 @@ void AssistantPageView::OnUiVisibilityChanged( ...@@ -238,8 +238,10 @@ void AssistantPageView::OnUiVisibilityChanged(
return; return;
} }
const bool prefer_voice = assistant_view_delegate_->IsTabletMode() || const bool prefer_voice =
assistant_view_delegate_->IsLaunchWithMicOpen(); assistant_view_delegate_->IsTabletMode() ||
assistant_view_delegate_->GetState()->launch_with_mic_open().value_or(
false);
if (!ash::assistant::util::IsVoiceEntryPoint(entry_point.value(), if (!ash::assistant::util::IsVoiceEntryPoint(entry_point.value(),
prefer_voice)) { prefer_voice)) {
NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true); NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
......
...@@ -119,8 +119,8 @@ void AssistantController::SendAssistantFeedback( ...@@ -119,8 +119,8 @@ void AssistantController::SendAssistantFeedback(
} }
void AssistantController::StartSpeakerIdEnrollmentFlow() { void AssistantController::StartSpeakerIdEnrollmentFlow() {
if (prefs_controller()->prefs()->GetInteger( if (state()->consent_status().value_or(
chromeos::assistant::prefs::kAssistantConsentStatus) == chromeos::assistant::prefs::ConsentStatus::kUnknown) ==
chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted) { chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted) {
// If activity control has been accepted, launch the enrollment flow. // If activity control has been accepted, launch the enrollment flow.
setup_controller()->StartOnboarding(false /* relaunch */, setup_controller()->StartOnboarding(false /* relaunch */,
......
...@@ -115,6 +115,8 @@ class ASH_EXPORT AssistantController ...@@ -115,6 +115,8 @@ class ASH_EXPORT AssistantController
void GetNavigableContentsFactory( void GetNavigableContentsFactory(
mojo::PendingReceiver<content::mojom::NavigableContentsFactory> receiver); mojo::PendingReceiver<content::mojom::NavigableContentsFactory> receiver);
AssistantStateBase* state() { return &assistant_prefs_controller_; }
AssistantAlarmTimerController* alarm_timer_controller() { AssistantAlarmTimerController* alarm_timer_controller() {
return &assistant_alarm_timer_controller_; return &assistant_alarm_timer_controller_;
} }
...@@ -131,10 +133,6 @@ class ASH_EXPORT AssistantController ...@@ -131,10 +133,6 @@ class ASH_EXPORT AssistantController
return &assistant_notification_controller_; return &assistant_notification_controller_;
} }
AssistantPrefsController* prefs_controller() {
return &assistant_prefs_controller_;
}
AssistantScreenContextController* screen_context_controller() { AssistantScreenContextController* screen_context_controller() {
return &assistant_screen_context_controller_; return &assistant_screen_context_controller_;
} }
......
...@@ -701,8 +701,7 @@ void AssistantInteractionController::OnUiVisible( ...@@ -701,8 +701,7 @@ void AssistantInteractionController::OnUiVisible(
assistant_controller_->ui_controller()->model()->visibility()); assistant_controller_->ui_controller()->model()->visibility());
const bool launch_with_mic_open = const bool launch_with_mic_open =
assistant_controller_->prefs_controller()->prefs()->GetBoolean( assistant_controller_->state()->launch_with_mic_open().value_or(false);
chromeos::assistant::prefs::kAssistantLaunchWithMicOpen);
const bool prefer_voice = launch_with_mic_open || IsTabletMode(); const bool prefer_voice = launch_with_mic_open || IsTabletMode();
// We don't explicitly start a new voice interaction if the entry point // We don't explicitly start a new voice interaction if the entry point
......
...@@ -205,8 +205,7 @@ void AssistantNotificationController::SetQuietMode(bool enabled) { ...@@ -205,8 +205,7 @@ void AssistantNotificationController::SetQuietMode(bool enabled) {
void AssistantNotificationController::OnNotificationAdded( void AssistantNotificationController::OnNotificationAdded(
const AssistantNotification* notification) { const AssistantNotification* notification) {
// Do not show system notifications if the setting is disabled. // Do not show system notifications if the setting is disabled.
if (!assistant_controller_->prefs_controller()->prefs()->GetBoolean( if (!assistant_controller_->state()->notification_enabled().value_or(true)) {
chromeos::assistant::prefs::kAssistantNotificationEnabled)) {
return; return;
} }
...@@ -221,8 +220,7 @@ void AssistantNotificationController::OnNotificationAdded( ...@@ -221,8 +220,7 @@ void AssistantNotificationController::OnNotificationAdded(
void AssistantNotificationController::OnNotificationUpdated( void AssistantNotificationController::OnNotificationUpdated(
const AssistantNotification* notification) { const AssistantNotification* notification) {
// Do not show system notifications if the setting is disabled. // Do not show system notifications if the setting is disabled.
if (!assistant_controller_->prefs_controller()->prefs()->GetBoolean( if (!assistant_controller_->state()->notification_enabled().value_or(true)) {
chromeos::assistant::prefs::kAssistantNotificationEnabled)) {
return; return;
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "ash/session/session_controller_impl.h" #include "ash/session/session_controller_impl.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/system/update/update_notification_controller.h"
#include "chromeos/services/assistant/public/cpp/assistant_prefs.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
...@@ -16,67 +18,92 @@ AssistantPrefsController::AssistantPrefsController() ...@@ -16,67 +18,92 @@ AssistantPrefsController::AssistantPrefsController()
AssistantPrefsController::~AssistantPrefsController() = default; AssistantPrefsController::~AssistantPrefsController() = default;
void AssistantPrefsController::AddObserver(AssistantPrefsObserver* observer) { void AssistantPrefsController::InitializeObserver(
InitObserver(observer); AssistantStateObserver* observer) {
observers_.AddObserver(observer); if (consent_status_.has_value())
observer->OnAssistantConsentStatusChanged(consent_status_.value());
} }
void AssistantPrefsController::RemoveObserver( void AssistantPrefsController::UpdateState() {
AssistantPrefsObserver* observer) { UpdateConsentStatus();
observers_.RemoveObserver(observer); UpdateHotwordAlwaysOn();
} UpdateLaunchWithMicOpen();
UpdateNotificationEnabled();
void AssistantPrefsController::InitObserver(AssistantPrefsObserver* observer) {
PrefService* primary_user_prefs =
Shell::Get()->session_controller()->GetPrimaryUserPrefService();
PrefService* active_prefs =
Shell::Get()->session_controller()->GetActivePrefService();
if (primary_user_prefs && primary_user_prefs == active_prefs) {
observer->OnAssistantConsentStatusUpdated(active_prefs->GetInteger(
chromeos::assistant::prefs::kAssistantConsentStatus));
}
}
PrefService* AssistantPrefsController::prefs() {
PrefService* primary_user_prefs =
Shell::Get()->session_controller()->GetPrimaryUserPrefService();
PrefService* active_prefs =
Shell::Get()->session_controller()->GetActivePrefService();
DCHECK_EQ(primary_user_prefs, active_prefs);
return primary_user_prefs;
} }
void AssistantPrefsController::OnActiveUserPrefServiceChanged( void AssistantPrefsController::OnActiveUserPrefServiceChanged(
PrefService* pref_service) { PrefService* pref_service) {
pref_change_registrar_.reset(); pref_change_registrar_.reset();
// If primary user is active, register pref change listeners. // Skip for non-primary user prefs.
PrefService* primary_user_prefs = PrefService* primary_user_prefs =
Shell::Get()->session_controller()->GetPrimaryUserPrefService(); Shell::Get()->session_controller()->GetPrimaryUserPrefService();
if (primary_user_prefs && primary_user_prefs == pref_service) { if (!primary_user_prefs || primary_user_prefs != pref_service)
for (auto& observer : observers_) return;
InitObserver(&observer);
// Register preference changes.
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(pref_service);
pref_change_registrar_->Add(
chromeos::assistant::prefs::kAssistantConsentStatus,
base::BindRepeating(&AssistantPrefsController::UpdateConsentStatus,
base::Unretained(this)));
pref_change_registrar_->Add(
chromeos::assistant::prefs::kAssistantHotwordAlwaysOn,
base::BindRepeating(&AssistantPrefsController::UpdateHotwordAlwaysOn,
base::Unretained(this)));
pref_change_registrar_->Add(
chromeos::assistant::prefs::kAssistantLaunchWithMicOpen,
base::BindRepeating(&AssistantPrefsController::UpdateLaunchWithMicOpen,
base::Unretained(this)));
pref_change_registrar_->Add(
chromeos::assistant::prefs::kAssistantNotificationEnabled,
base::BindRepeating(&AssistantPrefsController::UpdateNotificationEnabled,
base::Unretained(this)));
UpdateState();
}
void AssistantPrefsController::UpdateConsentStatus() {
auto consent_status = pref_change_registrar_->prefs()->GetInteger(
chromeos::assistant::prefs::kAssistantConsentStatus);
if (consent_status_.has_value() &&
consent_status_.value() == consent_status) {
return;
}
consent_status_ = consent_status;
for (auto& observer : observers_)
observer.OnAssistantConsentStatusChanged(consent_status_.value());
}
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); void AssistantPrefsController::UpdateHotwordAlwaysOn() {
pref_change_registrar_->Init(pref_service); auto hotword_always_on = pref_change_registrar_->prefs()->GetBoolean(
chromeos::assistant::prefs::kAssistantHotwordAlwaysOn);
if (hotword_always_on_.has_value() &&
hotword_always_on_.value() == hotword_always_on) {
return;
}
hotword_always_on_ = hotword_always_on;
}
pref_change_registrar_->Add( void AssistantPrefsController::UpdateLaunchWithMicOpen() {
chromeos::assistant::prefs::kAssistantConsentStatus, auto launch_with_mic_open = pref_change_registrar_->prefs()->GetBoolean(
base::BindRepeating(&AssistantPrefsController::NotifyConsentStatus, chromeos::assistant::prefs::kAssistantLaunchWithMicOpen);
base::Unretained(this))); if (launch_with_mic_open_.has_value() &&
launch_with_mic_open_.value() == launch_with_mic_open) {
return;
} }
launch_with_mic_open_ = launch_with_mic_open;
} }
void AssistantPrefsController::NotifyConsentStatus() { void AssistantPrefsController::UpdateNotificationEnabled() {
for (auto& observer : observers_) { auto notification_enabled = pref_change_registrar_->prefs()->GetBoolean(
observer.OnAssistantConsentStatusUpdated( chromeos::assistant::prefs::kAssistantNotificationEnabled);
Shell::Get() if (notification_enabled_.has_value() &&
->session_controller() notification_enabled_.value() == notification_enabled) {
->GetPrimaryUserPrefService() return;
->GetInteger(chromeos::assistant::prefs::kAssistantConsentStatus));
} }
notification_enabled_ = notification_enabled;
} }
} // namespace ash } // namespace ash
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define ASH_ASSISTANT_ASSISTANT_PREFS_CONTROLLER_H_ #define ASH_ASSISTANT_ASSISTANT_PREFS_CONTROLLER_H_
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "ash/public/cpp/assistant/assistant_state_base.h"
#include "ash/session/session_observer.h" #include "ash/session/session_observer.h"
#include "base/macros.h" #include "base/macros.h"
#include "chromeos/services/assistant/public/cpp/assistant_prefs.h" #include "chromeos/services/assistant/public/cpp/assistant_prefs.h"
...@@ -14,43 +15,36 @@ class PrefChangeRegistrar; ...@@ -14,43 +15,36 @@ class PrefChangeRegistrar;
namespace ash { namespace ash {
// A checked observer which receives Assistant prefs change. // Provide access of Assistant related preferences to clients in ash.
class ASH_EXPORT AssistantPrefsObserver : public base::CheckedObserver { class ASH_EXPORT AssistantPrefsController : public SessionObserver,
public: public AssistantStateBase {
AssistantPrefsObserver() = default;
~AssistantPrefsObserver() override = default;
virtual void OnAssistantConsentStatusUpdated(int consent_status) {}
private:
DISALLOW_COPY_AND_ASSIGN(AssistantPrefsObserver);
};
class ASH_EXPORT AssistantPrefsController : public SessionObserver {
public: public:
AssistantPrefsController(); AssistantPrefsController();
~AssistantPrefsController() override; ~AssistantPrefsController() override;
void AddObserver(AssistantPrefsObserver* observer); // AssistantStateBase:
void RemoveObserver(AssistantPrefsObserver* observer); void InitializeObserver(AssistantStateObserver* observer) override;
void InitObserver(AssistantPrefsObserver* observer);
PrefService* prefs();
private: private:
// Update pref cache and notify observers when primary user prefs becomes
// active.
void UpdateState();
// SessionObserver: // SessionObserver:
void OnActiveUserPrefServiceChanged(PrefService* pref_service) override; void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
// Called when the consent status is obtained from the pref service. // Called when the related preferences are obtained from the pref service.
void NotifyConsentStatus(); void UpdateConsentStatus();
void UpdateHotwordAlwaysOn();
void UpdateLaunchWithMicOpen();
void UpdateNotificationEnabled();
// TODO(b/138679823): Move related logics into AssistantStateBase.
// Observes user profile prefs for the Assistant. // Observes user profile prefs for the Assistant.
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
ScopedSessionObserver session_observer_; ScopedSessionObserver session_observer_;
base::ObserverList<AssistantPrefsObserver> observers_;
DISALLOW_COPY_AND_ASSIGN(AssistantPrefsController); DISALLOW_COPY_AND_ASSIGN(AssistantPrefsController);
}; };
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "ash/assistant/assistant_controller.h" #include "ash/assistant/assistant_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/test/ash_test_base.h" #include "ash/test/ash_test_base.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -16,13 +17,13 @@ ...@@ -16,13 +17,13 @@
namespace ash { namespace ash {
class TestAssistantPrefsObserver : public AssistantPrefsObserver { class TestAssistantPrefsObserver : public AssistantStateObserver {
public: public:
TestAssistantPrefsObserver() = default; TestAssistantPrefsObserver() = default;
~TestAssistantPrefsObserver() override = default; ~TestAssistantPrefsObserver() override = default;
// AssistantPrefsObserver: // AssistantPrefsObserver:
void OnAssistantConsentStatusUpdated(int consent_status) override { void OnAssistantConsentStatusChanged(int consent_status) override {
consent_status_ = consent_status; consent_status_ = consent_status;
} }
...@@ -46,7 +47,7 @@ class AssistantPrefsControllerTest : public AshTestBase { ...@@ -46,7 +47,7 @@ class AssistantPrefsControllerTest : public AshTestBase {
AshTestBase::SetUp(); AshTestBase::SetUp();
prefs_ = Shell::Get()->assistant_controller()->prefs_controller()->prefs(); prefs_ = Shell::Get()->session_controller()->GetPrimaryUserPrefService();
DCHECK(prefs_); DCHECK(prefs_);
observer_ = std::make_unique<TestAssistantPrefsObserver>(); observer_ = std::make_unique<TestAssistantPrefsObserver>();
...@@ -72,15 +73,13 @@ TEST_F(AssistantPrefsControllerTest, InitObserver) { ...@@ -72,15 +73,13 @@ TEST_F(AssistantPrefsControllerTest, InitObserver) {
// The observer class should get an instant notification about the current // The observer class should get an instant notification about the current
// pref value. // pref value.
Shell::Get()->assistant_controller()->prefs_controller()->AddObserver( Shell::Get()->assistant_controller()->state()->AddObserver(observer());
observer());
EXPECT_EQ(chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted, EXPECT_EQ(chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted,
observer()->consent_status()); observer()->consent_status());
} }
TEST_F(AssistantPrefsControllerTest, NotifyConsentStatus) { TEST_F(AssistantPrefsControllerTest, NotifyConsentStatus) {
Shell::Get()->assistant_controller()->prefs_controller()->AddObserver( Shell::Get()->assistant_controller()->state()->AddObserver(observer());
observer());
prefs()->SetInteger(chromeos::assistant::prefs::kAssistantConsentStatus, prefs()->SetInteger(chromeos::assistant::prefs::kAssistantConsentStatus,
chromeos::assistant::prefs::ConsentStatus::kUnauthorized); chromeos::assistant::prefs::ConsentStatus::kUnauthorized);
......
...@@ -58,8 +58,8 @@ void AssistantSetupController::OnDeepLinkReceived( ...@@ -58,8 +58,8 @@ void AssistantSetupController::OnDeepLinkReceived(
} }
void AssistantSetupController::OnOptInButtonPressed() { void AssistantSetupController::OnOptInButtonPressed() {
if (assistant_controller_->prefs_controller()->prefs()->GetInteger( if (assistant_controller_->state()->consent_status().value_or(
chromeos::assistant::prefs::kAssistantConsentStatus) == chromeos::assistant::prefs::ConsentStatus::kUnknown) ==
chromeos::assistant::prefs::ConsentStatus::kUnauthorized) { chromeos::assistant::prefs::ConsentStatus::kUnauthorized) {
assistant_controller_->OpenUrl(assistant::util::CreateLocalizedGURL( assistant_controller_->OpenUrl(assistant::util::CreateLocalizedGURL(
kGSuiteAdministratorInstructionsUrl)); kGSuiteAdministratorInstructionsUrl));
......
...@@ -81,6 +81,16 @@ void AssistantViewDelegateImpl::RemoveNotificationModelObserver( ...@@ -81,6 +81,16 @@ void AssistantViewDelegateImpl::RemoveNotificationModelObserver(
observer); observer);
} }
void AssistantViewDelegateImpl::AddStateObserver(
AssistantStateObserver* observer) {
assistant_controller_->state()->AddObserver(observer);
}
void AssistantViewDelegateImpl::RemoveStateObserver(
AssistantStateObserver* observer) {
assistant_controller_->state()->RemoveObserver(observer);
}
void AssistantViewDelegateImpl::AddUiModelObserver( void AssistantViewDelegateImpl::AddUiModelObserver(
AssistantUiModelObserver* observer) { AssistantUiModelObserver* observer) {
assistant_controller_->ui_controller()->AddModelObserver(observer); assistant_controller_->ui_controller()->AddModelObserver(observer);
...@@ -91,16 +101,6 @@ void AssistantViewDelegateImpl::RemoveUiModelObserver( ...@@ -91,16 +101,6 @@ void AssistantViewDelegateImpl::RemoveUiModelObserver(
assistant_controller_->ui_controller()->RemoveModelObserver(observer); assistant_controller_->ui_controller()->RemoveModelObserver(observer);
} }
void AssistantViewDelegateImpl::AddAssistantPrefsObserver(
AssistantPrefsObserver* observer) {
assistant_controller_->prefs_controller()->AddObserver(observer);
}
void AssistantViewDelegateImpl::RemoveAssistantPrefsObserver(
AssistantPrefsObserver* observer) {
assistant_controller_->prefs_controller()->RemoveObserver(observer);
}
CaptionBarDelegate* AssistantViewDelegateImpl::GetCaptionBarDelegate() { CaptionBarDelegate* AssistantViewDelegateImpl::GetCaptionBarDelegate() {
return assistant_controller_->ui_controller(); return assistant_controller_->ui_controller();
} }
...@@ -111,9 +111,8 @@ void AssistantViewDelegateImpl::DownloadImage( ...@@ -111,9 +111,8 @@ void AssistantViewDelegateImpl::DownloadImage(
assistant_controller_->DownloadImage(url, std::move(callback)); assistant_controller_->DownloadImage(url, std::move(callback));
} }
int AssistantViewDelegateImpl::GetConsentStatus() const { AssistantStateBase* AssistantViewDelegateImpl::GetState() const {
return assistant_controller_->prefs_controller()->prefs()->GetInteger( return assistant_controller_->state();
chromeos::assistant::prefs::kAssistantConsentStatus);
} }
::wm::CursorManager* AssistantViewDelegateImpl::GetCursorManager() { ::wm::CursorManager* AssistantViewDelegateImpl::GetCursorManager() {
...@@ -129,11 +128,6 @@ aura::Window* AssistantViewDelegateImpl::GetRootWindowForNewWindows() { ...@@ -129,11 +128,6 @@ aura::Window* AssistantViewDelegateImpl::GetRootWindowForNewWindows() {
return Shell::Get()->GetRootWindowForNewWindows(); return Shell::Get()->GetRootWindowForNewWindows();
} }
bool AssistantViewDelegateImpl::IsLaunchWithMicOpen() const {
return assistant_controller_->prefs_controller()->prefs()->GetBoolean(
chromeos::assistant::prefs::kAssistantLaunchWithMicOpen);
}
bool AssistantViewDelegateImpl::IsTabletMode() const { bool AssistantViewDelegateImpl::IsTabletMode() const {
return Shell::Get()->tablet_mode_controller()->InTabletMode(); return Shell::Get()->tablet_mode_controller()->InTabletMode();
} }
......
...@@ -39,21 +39,20 @@ class AssistantViewDelegateImpl : public AssistantViewDelegate { ...@@ -39,21 +39,20 @@ class AssistantViewDelegateImpl : public AssistantViewDelegate {
AssistantNotificationModelObserver* observer) override; AssistantNotificationModelObserver* observer) override;
void RemoveNotificationModelObserver( void RemoveNotificationModelObserver(
AssistantNotificationModelObserver* observer) override; AssistantNotificationModelObserver* observer) override;
void AddStateObserver(AssistantStateObserver* observer) override;
void RemoveStateObserver(AssistantStateObserver* observer) override;
void AddUiModelObserver(AssistantUiModelObserver* observer) override; void AddUiModelObserver(AssistantUiModelObserver* observer) override;
void RemoveUiModelObserver(AssistantUiModelObserver* observer) override; void RemoveUiModelObserver(AssistantUiModelObserver* observer) override;
void AddAssistantPrefsObserver(AssistantPrefsObserver* observer) override;
void RemoveAssistantPrefsObserver(AssistantPrefsObserver* observer) override;
CaptionBarDelegate* GetCaptionBarDelegate() override; CaptionBarDelegate* GetCaptionBarDelegate() override;
void DownloadImage( void DownloadImage(
const GURL& url, const GURL& url,
AssistantImageDownloader::DownloadCallback callback) override; AssistantImageDownloader::DownloadCallback callback) override;
int GetConsentStatus() const override; AssistantStateBase* GetState() const override;
::wm::CursorManager* GetCursorManager() override; ::wm::CursorManager* GetCursorManager() override;
void GetNavigableContentsFactoryForView( void GetNavigableContentsFactoryForView(
mojo::PendingReceiver<content::mojom::NavigableContentsFactory> receiver) mojo::PendingReceiver<content::mojom::NavigableContentsFactory> receiver)
override; override;
aura::Window* GetRootWindowForNewWindows() override; aura::Window* GetRootWindowForNewWindows() override;
bool IsLaunchWithMicOpen() const override;
bool IsTabletMode() const override; bool IsTabletMode() const override;
void OnDialogPlateButtonPressed(AssistantButtonId id) override; void OnDialogPlateButtonPressed(AssistantButtonId id) override;
void OnDialogPlateContentsCommitted(const std::string& text) override; void OnDialogPlateContentsCommitted(const std::string& text) override;
......
...@@ -113,11 +113,9 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate { ...@@ -113,11 +113,9 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate {
virtual void AddUiModelObserver(AssistantUiModelObserver* observer) = 0; virtual void AddUiModelObserver(AssistantUiModelObserver* observer) = 0;
virtual void RemoveUiModelObserver(AssistantUiModelObserver* observer) = 0; virtual void RemoveUiModelObserver(AssistantUiModelObserver* observer) = 0;
// Adds/removes the Assistant prefs observer associated with the view // Adds/removes the state observer associated with the view delegate.
// delegate. virtual void AddStateObserver(AssistantStateObserver* observer) = 0;
virtual void AddAssistantPrefsObserver(AssistantPrefsObserver* observer) = 0; virtual void RemoveStateObserver(AssistantStateObserver* observer) = 0;
virtual void RemoveAssistantPrefsObserver(
AssistantPrefsObserver* observer) = 0;
// Gets the caption bar delegate associated with the view delegate. // Gets the caption bar delegate associated with the view delegate.
virtual CaptionBarDelegate* GetCaptionBarDelegate() = 0; virtual CaptionBarDelegate* GetCaptionBarDelegate() = 0;
...@@ -129,8 +127,7 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate { ...@@ -129,8 +127,7 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate {
const GURL& url, const GURL& url,
AssistantImageDownloader::DownloadCallback callback) = 0; AssistantImageDownloader::DownloadCallback callback) = 0;
// Returns the status of the user's consent. virtual AssistantStateBase* GetState() const = 0;
virtual int GetConsentStatus() const = 0;
// Returns the cursor_manager. // Returns the cursor_manager.
virtual ::wm::CursorManager* GetCursorManager() = 0; virtual ::wm::CursorManager* GetCursorManager() = 0;
...@@ -144,9 +141,6 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate { ...@@ -144,9 +141,6 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantViewDelegate {
// Returns the root window that newly created windows should be added to. // Returns the root window that newly created windows should be added to.
virtual aura::Window* GetRootWindowForNewWindows() = 0; virtual aura::Window* GetRootWindowForNewWindows() = 0;
// Returns true if user prefers to start with voice interaction.
virtual bool IsLaunchWithMicOpen() const = 0;
// Returns true if in tablet mode. // Returns true if in tablet mode.
virtual bool IsTabletMode() const = 0; virtual bool IsTabletMode() const = 0;
......
...@@ -45,11 +45,11 @@ AssistantFooterView::AssistantFooterView(AssistantViewDelegate* delegate) ...@@ -45,11 +45,11 @@ AssistantFooterView::AssistantFooterView(AssistantViewDelegate* delegate)
&AssistantFooterView::OnAnimationEnded, &AssistantFooterView::OnAnimationEnded,
base::Unretained(this)))) { base::Unretained(this)))) {
InitLayout(); InitLayout();
delegate_->AddAssistantPrefsObserver(this); delegate_->AddStateObserver(this);
} }
AssistantFooterView::~AssistantFooterView() { AssistantFooterView::~AssistantFooterView() {
delegate_->RemoveAssistantPrefsObserver(this); delegate_->RemoveStateObserver(this);
} }
const char* AssistantFooterView::GetClassName() const { const char* AssistantFooterView::GetClassName() const {
...@@ -69,7 +69,8 @@ void AssistantFooterView::InitLayout() { ...@@ -69,7 +69,8 @@ void AssistantFooterView::InitLayout() {
// Initial view state is based on user consent state. // Initial view state is based on user consent state.
const bool consent_given = const bool consent_given =
delegate_->GetConsentStatus() == delegate_->GetState()->consent_status().value_or(
chromeos::assistant::prefs::ConsentStatus::kUnknown) ==
chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted; chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted;
// Suggestion container. // Suggestion container.
...@@ -97,7 +98,7 @@ void AssistantFooterView::InitLayout() { ...@@ -97,7 +98,7 @@ void AssistantFooterView::InitLayout() {
AddChildView(opt_in_view_); AddChildView(opt_in_view_);
} }
void AssistantFooterView::OnAssistantConsentStatusUpdated(int consent_status) { void AssistantFooterView::OnAssistantConsentStatusChanged(int consent_status) {
using assistant::util::CreateLayerAnimationSequence; using assistant::util::CreateLayerAnimationSequence;
using assistant::util::CreateOpacityElement; using assistant::util::CreateOpacityElement;
using assistant::util::StartLayerAnimationSequence; using assistant::util::StartLayerAnimationSequence;
...@@ -152,7 +153,8 @@ void AssistantFooterView::OnAnimationStarted( ...@@ -152,7 +153,8 @@ void AssistantFooterView::OnAnimationStarted(
bool AssistantFooterView::OnAnimationEnded( bool AssistantFooterView::OnAnimationEnded(
const ui::CallbackLayerAnimationObserver& observer) { const ui::CallbackLayerAnimationObserver& observer) {
const bool consent_given = const bool consent_given =
delegate_->GetConsentStatus() == delegate_->GetState()->consent_status().value_or(
chromeos::assistant::prefs::ConsentStatus::kUnknown) ==
chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted; chromeos::assistant::prefs::ConsentStatus::kActivityControlAccepted;
// Only the view relevant to our consent state should process events. // Only the view relevant to our consent state should process events.
......
...@@ -25,7 +25,7 @@ class SuggestionContainerView; ...@@ -25,7 +25,7 @@ class SuggestionContainerView;
class COMPONENT_EXPORT(ASSISTANT_UI) AssistantFooterView class COMPONENT_EXPORT(ASSISTANT_UI) AssistantFooterView
: public views::View, : public views::View,
public AssistantPrefsObserver { public AssistantStateObserver {
public: public:
explicit AssistantFooterView(AssistantViewDelegate* delegate); explicit AssistantFooterView(AssistantViewDelegate* delegate);
~AssistantFooterView() override; ~AssistantFooterView() override;
...@@ -35,8 +35,8 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantFooterView ...@@ -35,8 +35,8 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantFooterView
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
int GetHeightForWidth(int width) const override; int GetHeightForWidth(int width) const override;
// AssistantPrefsObserver: // AssistantStateObserver:
void OnAssistantConsentStatusUpdated(int consent_status) override; void OnAssistantConsentStatusChanged(int consent_status) override;
private: private:
void InitLayout(); void InitLayout();
......
...@@ -90,11 +90,11 @@ class AssistantOptInContainer : public views::Button { ...@@ -90,11 +90,11 @@ class AssistantOptInContainer : public views::Button {
AssistantOptInView::AssistantOptInView(AssistantViewDelegate* delegate) AssistantOptInView::AssistantOptInView(AssistantViewDelegate* delegate)
: delegate_(delegate) { : delegate_(delegate) {
InitLayout(); InitLayout();
delegate_->AddAssistantPrefsObserver(this); delegate_->AddStateObserver(this);
} }
AssistantOptInView::~AssistantOptInView() { AssistantOptInView::~AssistantOptInView() {
delegate_->RemoveAssistantPrefsObserver(this); delegate_->RemoveStateObserver(this);
} }
const char* AssistantOptInView::GetClassName() const { const char* AssistantOptInView::GetClassName() const {
...@@ -114,7 +114,7 @@ void AssistantOptInView::ButtonPressed(views::Button* sender, ...@@ -114,7 +114,7 @@ void AssistantOptInView::ButtonPressed(views::Button* sender,
delegate_->OnOptInButtonPressed(); delegate_->OnOptInButtonPressed();
} }
void AssistantOptInView::OnAssistantConsentStatusUpdated(int consent_status) { void AssistantOptInView::OnAssistantConsentStatusChanged(int consent_status) {
UpdateLabel(consent_status); UpdateLabel(consent_status);
} }
...@@ -152,7 +152,8 @@ void AssistantOptInView::InitLayout() { ...@@ -152,7 +152,8 @@ void AssistantOptInView::InitLayout() {
container_->AddChildView(label_); container_->AddChildView(label_);
container_->SetFocusForPlatform(); container_->SetFocusForPlatform();
UpdateLabel(delegate_->GetConsentStatus()); UpdateLabel(delegate_->GetState()->consent_status().value_or(
chromeos::assistant::prefs::ConsentStatus::kUnknown));
} }
void AssistantOptInView::UpdateLabel(int consent_status) { void AssistantOptInView::UpdateLabel(int consent_status) {
......
...@@ -23,7 +23,7 @@ class AssistantViewDelegate; ...@@ -23,7 +23,7 @@ class AssistantViewDelegate;
class COMPONENT_EXPORT(ASSISTANT_UI) AssistantOptInView class COMPONENT_EXPORT(ASSISTANT_UI) AssistantOptInView
: public views::View, : public views::View,
public views::ButtonListener, public views::ButtonListener,
public AssistantPrefsObserver { public AssistantStateObserver {
public: public:
explicit AssistantOptInView(AssistantViewDelegate* delegate_); explicit AssistantOptInView(AssistantViewDelegate* delegate_);
~AssistantOptInView() override; ~AssistantOptInView() override;
...@@ -36,8 +36,8 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantOptInView ...@@ -36,8 +36,8 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantOptInView
// views::ButtonListener: // views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override; void ButtonPressed(views::Button* sender, const ui::Event& event) override;
// AssistantPrefsObserver: // AssistantStateObserver:
void OnAssistantConsentStatusUpdated(int consent_status) override; void OnAssistantConsentStatusChanged(int consent_status) override;
private: private:
void InitLayout(); void InitLayout();
......
...@@ -44,4 +44,13 @@ std::string AssistantStateBase::ToString() const { ...@@ -44,4 +44,13 @@ std::string AssistantStateBase::ToString() const {
return result.str(); return result.str();
} }
void AssistantStateBase::AddObserver(AssistantStateObserver* observer) {
observers_.AddObserver(observer);
InitializeObserver(observer);
}
void AssistantStateBase::RemoveObserver(AssistantStateObserver* observer) {
observers_.RemoveObserver(observer);
}
} // namespace ash } // namespace ash
...@@ -13,6 +13,18 @@ ...@@ -13,6 +13,18 @@
namespace ash { namespace ash {
// A checked observer which receives Assistant state change.
class ASH_PUBLIC_EXPORT AssistantStateObserver : public base::CheckedObserver {
public:
AssistantStateObserver() = default;
~AssistantStateObserver() override = default;
virtual void OnAssistantConsentStatusChanged(int consent_status) {}
private:
DISALLOW_COPY_AND_ASSIGN(AssistantStateObserver);
};
// Plain data class that holds Assistant related prefs and states. This is // Plain data class that holds Assistant related prefs and states. This is
// shared by both the controller that controlls these values and client proxy // shared by both the controller that controlls these values and client proxy
// that caches these values locally. Please do not use this object directly, // that caches these values locally. Please do not use this object directly,
...@@ -31,6 +43,8 @@ class ASH_PUBLIC_EXPORT AssistantStateBase { ...@@ -31,6 +43,8 @@ class ASH_PUBLIC_EXPORT AssistantStateBase {
return settings_enabled_; return settings_enabled_;
} }
const base::Optional<int>& consent_status() const { return consent_status_; }
const base::Optional<bool>& context_enabled() const { const base::Optional<bool>& context_enabled() const {
return context_enabled_; return context_enabled_;
} }
...@@ -39,6 +53,18 @@ class ASH_PUBLIC_EXPORT AssistantStateBase { ...@@ -39,6 +53,18 @@ class ASH_PUBLIC_EXPORT AssistantStateBase {
return hotword_enabled_; return hotword_enabled_;
} }
const base::Optional<bool>& hotword_always_on() const {
return hotword_always_on_;
}
const base::Optional<bool>& launch_with_mic_open() const {
return launch_with_mic_open_;
}
const base::Optional<bool>& notification_enabled() const {
return notification_enabled_;
}
const base::Optional<mojom::AssistantAllowedState>& allowed_state() const { const base::Optional<mojom::AssistantAllowedState>& allowed_state() const {
return allowed_state_; return allowed_state_;
} }
...@@ -55,13 +81,21 @@ class ASH_PUBLIC_EXPORT AssistantStateBase { ...@@ -55,13 +81,21 @@ class ASH_PUBLIC_EXPORT AssistantStateBase {
std::string ToString() const; std::string ToString() const;
void AddObserver(AssistantStateObserver* observer);
void RemoveObserver(AssistantStateObserver* observer);
virtual void InitializeObserver(AssistantStateObserver* observer) {}
protected: protected:
base::Optional<mojom::VoiceInteractionState> voice_interaction_state_; base::Optional<mojom::VoiceInteractionState> voice_interaction_state_;
// TODO(b/138679823): Maybe remove Optional for preference values.
// Whether voice interaction is enabled in system settings. nullopt if the // Whether voice interaction is enabled in system settings. nullopt if the
// data is not available yet. // data is not available yet.
base::Optional<bool> settings_enabled_; base::Optional<bool> settings_enabled_;
// The status of the user's consent. nullopt if the data is not available yet.
base::Optional<int> consent_status_;
// Whether screen context is enabled. nullopt if the data is not available // Whether screen context is enabled. nullopt if the data is not available
// yet. // yet.
base::Optional<bool> context_enabled_; base::Optional<bool> context_enabled_;
...@@ -69,6 +103,16 @@ class ASH_PUBLIC_EXPORT AssistantStateBase { ...@@ -69,6 +103,16 @@ class ASH_PUBLIC_EXPORT AssistantStateBase {
// Whether hotword listening is enabled. // Whether hotword listening is enabled.
base::Optional<bool> hotword_enabled_; base::Optional<bool> hotword_enabled_;
// Whether hotword listening is always on/only with power source. nullopt
// if the data is not available yet.
base::Optional<bool> hotword_always_on_;
// Whether the Assistant should launch with mic open;
base::Optional<bool> launch_with_mic_open_;
// Whether notification is enabled.
base::Optional<bool> notification_enabled_;
// Whether voice interaction feature is allowed or disallowed for what reason. // Whether voice interaction feature is allowed or disallowed for what reason.
// nullopt if the data is not available yet. // nullopt if the data is not available yet.
base::Optional<mojom::AssistantAllowedState> allowed_state_; base::Optional<mojom::AssistantAllowedState> allowed_state_;
...@@ -82,6 +126,8 @@ class ASH_PUBLIC_EXPORT AssistantStateBase { ...@@ -82,6 +126,8 @@ class ASH_PUBLIC_EXPORT AssistantStateBase {
// available yet. // available yet.
base::Optional<bool> locked_full_screen_enabled_; base::Optional<bool> locked_full_screen_enabled_;
base::ObserverList<AssistantStateObserver> observers_;
private: private:
DISALLOW_COPY_AND_ASSIGN(AssistantStateBase); DISALLOW_COPY_AND_ASSIGN(AssistantStateBase);
}; };
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
namespace ash { namespace ash {
// TODO(b/138679823): Merge this with AssistantPrefsController.
class ASH_PUBLIC_EXPORT VoiceInteractionController class ASH_PUBLIC_EXPORT VoiceInteractionController
: public mojom::VoiceInteractionController, : public mojom::VoiceInteractionController,
public AssistantStateBase { public AssistantStateBase {
......
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