David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 1 | // 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 Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 9 | #include "ash/assistant/assistant_controller.h" |
| 10 | #include "ash/assistant/assistant_ui_controller.h" |
David Black | 4c6b9e2 | 2018-08-27 20:31:31 | [diff] [blame^] | 11 | #include "ash/assistant/util/assistant_util.h" |
David Black | d08c051 | 2018-08-21 17:18:26 | [diff] [blame] | 12 | #include "ash/assistant/util/deep_link_util.h" |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 13 | #include "ash/shell.h" |
| 14 | #include "ash/strings/grit/ash_strings.h" |
| 15 | #include "ash/voice_interaction/voice_interaction_controller.h" |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 16 | #include "base/rand_util.h" |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 17 | #include "chromeos/services/assistant/public/mojom/assistant.mojom.h" |
| 18 | #include "ui/base/l10n/l10n_util.h" |
| 19 | |
| 20 | namespace ash { |
| 21 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | // Conversation starters. |
| 25 | constexpr int kNumOfConversationStarters = 3; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | AssistantCacheController::AssistantCacheController( |
| 30 | AssistantController* assistant_controller) |
| 31 | : assistant_controller_(assistant_controller), |
| 32 | voice_interaction_binding_(this) { |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 33 | UpdateConversationStarters(); |
| 34 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 35 | assistant_controller_->AddObserver(this); |
| 36 | |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 37 | // 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 Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 43 | AssistantCacheController::~AssistantCacheController() { |
| 44 | assistant_controller_->RemoveObserver(this); |
| 45 | } |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 46 | |
| 47 | void AssistantCacheController::AddModelObserver( |
| 48 | AssistantCacheModelObserver* observer) { |
| 49 | model_.AddObserver(observer); |
| 50 | } |
| 51 | |
| 52 | void AssistantCacheController::RemoveModelObserver( |
| 53 | AssistantCacheModelObserver* observer) { |
| 54 | model_.RemoveObserver(observer); |
| 55 | } |
| 56 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 57 | void AssistantCacheController::OnAssistantControllerConstructed() { |
| 58 | assistant_controller_->ui_controller()->AddModelObserver(this); |
| 59 | } |
| 60 | |
| 61 | void AssistantCacheController::OnAssistantControllerDestroying() { |
| 62 | assistant_controller_->ui_controller()->RemoveModelObserver(this); |
| 63 | } |
| 64 | |
David Black | 4c6b9e2 | 2018-08-27 20:31:31 | [diff] [blame^] | 65 | void 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 Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 72 | UpdateConversationStarters(); |
| 73 | } |
| 74 | |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 75 | void 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. |
| 81 | void AssistantCacheController::UpdateConversationStarters() { |
| 82 | using namespace chromeos::assistant::mojom; |
| 83 | |
| 84 | std::vector<AssistantSuggestionPtr> conversation_starters; |
| 85 | |
David Black | d08c051 | 2018-08-21 17:18:26 | [diff] [blame] | 86 | auto AddConversationStarter = [&conversation_starters]( |
| 87 | int message_id, GURL action_url = GURL()) { |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 88 | AssistantSuggestionPtr starter = AssistantSuggestion::New(); |
| 89 | starter->text = l10n_util::GetStringUTF8(message_id); |
David Black | d08c051 | 2018-08-21 17:18:26 | [diff] [blame] | 90 | starter->action_url = action_url; |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 91 | conversation_starters.push_back(std::move(starter)); |
| 92 | }; |
| 93 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 94 | // Always show the "What can you do?" conversation starter. |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 95 | AddConversationStarter(IDS_ASH_ASSISTANT_CHIP_WHAT_CAN_YOU_DO); |
| 96 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 97 | // Always show the "What's on my screen?" conversation starter (if enabled). |
David Black | d08c051 | 2018-08-21 17:18:26 | [diff] [blame] | 98 | if (Shell::Get()->voice_interaction_controller()->context_enabled()) { |
| 99 | AddConversationStarter(IDS_ASH_ASSISTANT_CHIP_WHATS_ON_MY_SCREEN, |
| 100 | assistant::util::CreateWhatsOnMyScreenDeepLink()); |
| 101 | } |
David Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 102 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 103 | // 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 Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 113 | |
David Black | 02974f8 | 2018-08-21 22:56:05 | [diff] [blame] | 114 | 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 Black | 6b4b1ad2 | 2018-08-21 02:45:08 | [diff] [blame] | 122 | |
| 123 | model_.SetConversationStarters(std::move(conversation_starters)); |
| 124 | } |
| 125 | |
| 126 | } // namespace ash |