blob: c3f0423777d51976a77453303dd02bd287accd29 [file] [log] [blame]
David Black6b4b1ad22018-08-21 02:45:081// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/assistant/assistant_cache_controller.h"
6
7#include <vector>
8
David Black02974f82018-08-21 22:56:059#include "ash/assistant/assistant_controller.h"
10#include "ash/assistant/assistant_ui_controller.h"
David Black4c6b9e22018-08-27 20:31:3111#include "ash/assistant/util/assistant_util.h"
David Blackd08c0512018-08-21 17:18:2612#include "ash/assistant/util/deep_link_util.h"
David Black6b4b1ad22018-08-21 02:45:0813#include "ash/shell.h"
14#include "ash/strings/grit/ash_strings.h"
15#include "ash/voice_interaction/voice_interaction_controller.h"
David Black02974f82018-08-21 22:56:0516#include "base/rand_util.h"
David Black6b4b1ad22018-08-21 02:45:0817#include "chromeos/services/assistant/public/mojom/assistant.mojom.h"
18#include "ui/base/l10n/l10n_util.h"
19
20namespace ash {
21
David Black02974f82018-08-21 22:56:0522namespace {
23
24// Conversation starters.
25constexpr int kNumOfConversationStarters = 3;
26
27} // namespace
28
29AssistantCacheController::AssistantCacheController(
30 AssistantController* assistant_controller)
31 : assistant_controller_(assistant_controller),
32 voice_interaction_binding_(this) {
David Black6b4b1ad22018-08-21 02:45:0833 UpdateConversationStarters();
34
David Black02974f82018-08-21 22:56:0535 assistant_controller_->AddObserver(this);
36
David Black6b4b1ad22018-08-21 02:45:0837 // Bind to observe changes to screen context preference.
38 mojom::VoiceInteractionObserverPtr ptr;
39 voice_interaction_binding_.Bind(mojo::MakeRequest(&ptr));
40 Shell::Get()->voice_interaction_controller()->AddObserver(std::move(ptr));
41}
42
David Black02974f82018-08-21 22:56:0543AssistantCacheController::~AssistantCacheController() {
44 assistant_controller_->RemoveObserver(this);
45}
David Black6b4b1ad22018-08-21 02:45:0846
47void AssistantCacheController::AddModelObserver(
48 AssistantCacheModelObserver* observer) {
49 model_.AddObserver(observer);
50}
51
52void AssistantCacheController::RemoveModelObserver(
53 AssistantCacheModelObserver* observer) {
54 model_.RemoveObserver(observer);
55}
56
David Black02974f82018-08-21 22:56:0557void AssistantCacheController::OnAssistantControllerConstructed() {
58 assistant_controller_->ui_controller()->AddModelObserver(this);
59}
60
61void AssistantCacheController::OnAssistantControllerDestroying() {
62 assistant_controller_->ui_controller()->RemoveModelObserver(this);
63}
64
David Black4c6b9e22018-08-27 20:31:3165void AssistantCacheController::OnUiVisibilityChanged(
66 AssistantVisibility new_visibility,
67 AssistantVisibility old_visibility,
68 AssistantSource source) {
69 // When Assistant is finishing a session, we update our cache of conversation
70 // starters so that they're fresh for the next launch.
71 if (assistant::util::IsFinishingSession(new_visibility))
David Black02974f82018-08-21 22:56:0572 UpdateConversationStarters();
73}
74
David Black6b4b1ad22018-08-21 02:45:0875void AssistantCacheController::OnVoiceInteractionContextEnabled(bool enabled) {
76 UpdateConversationStarters();
77}
78
79// TODO(dmblack): The conversation starter cache should receive its contents
80// from the server. Hard-coding for the time being.
81void AssistantCacheController::UpdateConversationStarters() {
82 using namespace chromeos::assistant::mojom;
83
84 std::vector<AssistantSuggestionPtr> conversation_starters;
85
David Blackd08c0512018-08-21 17:18:2686 auto AddConversationStarter = [&conversation_starters](
87 int message_id, GURL action_url = GURL()) {
David Black6b4b1ad22018-08-21 02:45:0888 AssistantSuggestionPtr starter = AssistantSuggestion::New();
89 starter->text = l10n_util::GetStringUTF8(message_id);
David Blackd08c0512018-08-21 17:18:2690 starter->action_url = action_url;
David Black6b4b1ad22018-08-21 02:45:0891 conversation_starters.push_back(std::move(starter));
92 };
93
David Black02974f82018-08-21 22:56:0594 // Always show the "What can you do?" conversation starter.
David Black6b4b1ad22018-08-21 02:45:0895 AddConversationStarter(IDS_ASH_ASSISTANT_CHIP_WHAT_CAN_YOU_DO);
96
David Black02974f82018-08-21 22:56:0597 // Always show the "What's on my screen?" conversation starter (if enabled).
David Blackd08c0512018-08-21 17:18:2698 if (Shell::Get()->voice_interaction_controller()->context_enabled()) {
99 AddConversationStarter(IDS_ASH_ASSISTANT_CHIP_WHATS_ON_MY_SCREEN,
100 assistant::util::CreateWhatsOnMyScreenDeepLink());
101 }
David Black6b4b1ad22018-08-21 02:45:08102
David Black02974f82018-08-21 22:56:05103 // The rest of the conversation starters will be shuffled...
104 int shuffled_message_ids[] = {
105 IDS_ASH_ASSISTANT_CHIP_IM_BORED,
106 IDS_ASH_ASSISTANT_CHIP_OPEN_FILES,
107 IDS_ASH_ASSISTANT_CHIP_PLAY_MUSIC,
108 IDS_ASH_ASSISTANT_CHIP_SEND_AN_EMAIL,
109 IDS_ASH_ASSISTANT_CHIP_SET_A_REMINDER,
110 IDS_ASH_ASSISTANT_CHIP_WHATS_ON_MY_CALENDAR,
111 IDS_ASH_ASSISTANT_CHIP_WHATS_THE_WEATHER,
112 };
David Black6b4b1ad22018-08-21 02:45:08113
David Black02974f82018-08-21 22:56:05114 base::RandomShuffle(std::begin(shuffled_message_ids),
115 std::end(shuffled_message_ids));
116
117 // ...and will be added until we reach |kNumOfConversationStarters|.
118 for (int i = 0; conversation_starters.size() < kNumOfConversationStarters;
119 ++i) {
120 AddConversationStarter(shuffled_message_ids[i]);
121 }
David Black6b4b1ad22018-08-21 02:45:08122
123 model_.SetConversationStarters(std::move(conversation_starters));
124}
125
126} // namespace ash