David Black | 71ab74d | 2018-04-12 06:52:31 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 5 | #include "ash/assistant/assistant_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 6 | |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 7 | #include "ash/assistant/model/assistant_interaction_model_observer.h" |
David Black | 7a1b66ff | 2018-05-11 21:48:25 | [diff] [blame] | 8 | #include "ash/assistant/model/assistant_query.h" |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 9 | #include "ash/assistant/model/assistant_ui_element.h" |
David Black | f7f8d201 | 2018-04-25 19:48:02 | [diff] [blame] | 10 | #include "ash/assistant/ui/assistant_bubble.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 11 | #include "ash/session/session_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 12 | #include "ash/shell.h" |
| 13 | #include "ash/shell_delegate.h" |
David Black | 46b99990 | 2018-05-16 19:59:02 | [diff] [blame] | 14 | #include "ash/system/toast/toast_data.h" |
| 15 | #include "ash/system/toast/toast_manager.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 16 | #include "base/bind.h" |
| 17 | #include "base/memory/scoped_refptr.h" |
David Black | 46b99990 | 2018-05-16 19:59:02 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 19 | #include "base/unguessable_token.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 20 | #include "ui/snapshot/snapshot.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 21 | |
| 22 | namespace ash { |
| 23 | |
David Black | 46b99990 | 2018-05-16 19:59:02 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Toast ----------------------------------------------------------------------- |
| 27 | |
| 28 | constexpr int kToastDurationMs = 2500; |
| 29 | constexpr char kUnboundServiceToastId[] = |
| 30 | "assistant_controller_unbound_service"; |
| 31 | |
| 32 | // TODO(b/77638210): Localize string. |
| 33 | constexpr char kSomethingWentWrong[] = |
| 34 | "Something went wrong. Try again in a few seconds."; |
| 35 | |
| 36 | void ShowToast(const std::string& id, const std::string& text) { |
| 37 | ToastData toast(id, base::UTF8ToUTF16(text), kToastDurationMs, base::nullopt); |
| 38 | Shell::Get()->toast_manager()->Show(toast); |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | // AssistantController --------------------------------------------------------- |
| 44 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 45 | AssistantController::AssistantController() |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 46 | : assistant_event_subscriber_binding_(this), |
David Black | f7f8d201 | 2018-04-25 19:48:02 | [diff] [blame] | 47 | assistant_bubble_(std::make_unique<AssistantBubble>(this)) { |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 48 | AddInteractionModelObserver(this); |
Qiang Xu | bd6d84e7 | 2018-05-09 01:31:51 | [diff] [blame] | 49 | Shell::Get()->highlighter_controller()->AddObserver(this); |
David Black | 71ab74d | 2018-04-12 06:52:31 | [diff] [blame] | 50 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 51 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 52 | AssistantController::~AssistantController() { |
Qiang Xu | bd6d84e7 | 2018-05-09 01:31:51 | [diff] [blame] | 53 | Shell::Get()->highlighter_controller()->RemoveObserver(this); |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 54 | RemoveInteractionModelObserver(this); |
| 55 | |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 56 | assistant_controller_bindings_.CloseAllBindings(); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 57 | assistant_event_subscriber_binding_.Close(); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 58 | } |
| 59 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 60 | void AssistantController::BindRequest( |
| 61 | mojom::AssistantControllerRequest request) { |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 62 | assistant_controller_bindings_.AddBinding(this, std::move(request)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 63 | } |
| 64 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 65 | void AssistantController::SetAssistant( |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 66 | chromeos::assistant::mojom::AssistantPtr assistant) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 67 | assistant_ = std::move(assistant); |
| 68 | |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 69 | // Subscribe to Assistant events. |
| 70 | chromeos::assistant::mojom::AssistantEventSubscriberPtr ptr; |
| 71 | assistant_event_subscriber_binding_.Bind(mojo::MakeRequest(&ptr)); |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 72 | assistant_->AddAssistantEventSubscriber(std::move(ptr)); |
| 73 | } |
| 74 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 75 | void AssistantController::SetAssistantCardRenderer( |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 76 | mojom::AssistantCardRendererPtr assistant_card_renderer) { |
| 77 | assistant_card_renderer_ = std::move(assistant_card_renderer); |
| 78 | } |
| 79 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame^] | 80 | void AssistantController::SetAssistantImageDownloader( |
| 81 | mojom::AssistantImageDownloaderPtr assistant_image_downloader) { |
| 82 | assistant_image_downloader_ = std::move(assistant_image_downloader); |
| 83 | } |
| 84 | |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 85 | void AssistantController::RequestScreenshot( |
| 86 | const gfx::Rect& rect, |
| 87 | RequestScreenshotCallback callback) { |
| 88 | // TODO(muyuanli): handle multi-display when assistant's behavior is defined. |
| 89 | auto* root_window = Shell::GetPrimaryRootWindow(); |
| 90 | gfx::Rect source_rect = |
| 91 | rect.IsEmpty() ? gfx::Rect(root_window->bounds().size()) : rect; |
| 92 | ui::GrabWindowSnapshotAsyncJPEG( |
| 93 | root_window, source_rect, |
| 94 | base::BindRepeating( |
| 95 | [](RequestScreenshotCallback callback, |
| 96 | scoped_refptr<base::RefCountedMemory> data) { |
| 97 | std::move(callback).Run(std::vector<uint8_t>( |
| 98 | data->front(), data->front() + data->size())); |
| 99 | }, |
| 100 | base::Passed(&callback))); |
| 101 | } |
| 102 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 103 | void AssistantController::RenderCard( |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 104 | const base::UnguessableToken& id_token, |
| 105 | mojom::AssistantCardParamsPtr params, |
| 106 | mojom::AssistantCardRenderer::RenderCallback callback) { |
| 107 | DCHECK(assistant_card_renderer_); |
| 108 | |
| 109 | const mojom::UserSession* user_session = |
| 110 | Shell::Get()->session_controller()->GetUserSession(0); |
| 111 | |
| 112 | if (!user_session) { |
| 113 | LOG(WARNING) << "Unable to retrieve active user session."; |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | AccountId account_id = user_session->user_info->account_id; |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 118 | assistant_card_renderer_->Render(account_id, id_token, std::move(params), |
| 119 | std::move(callback)); |
| 120 | } |
| 121 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 122 | void AssistantController::ReleaseCard(const base::UnguessableToken& id_token) { |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 123 | DCHECK(assistant_card_renderer_); |
| 124 | assistant_card_renderer_->Release(id_token); |
| 125 | } |
| 126 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 127 | void AssistantController::ReleaseCards( |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 128 | const std::vector<base::UnguessableToken>& id_tokens) { |
| 129 | DCHECK(assistant_card_renderer_); |
| 130 | assistant_card_renderer_->ReleaseAll(id_tokens); |
| 131 | } |
| 132 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame^] | 133 | void AssistantController::DownloadImage( |
| 134 | const GURL& url, |
| 135 | mojom::AssistantImageDownloader::DownloadCallback callback) { |
| 136 | DCHECK(assistant_image_downloader_); |
| 137 | |
| 138 | const mojom::UserSession* user_session = |
| 139 | Shell::Get()->session_controller()->GetUserSession(0); |
| 140 | |
| 141 | if (!user_session) { |
| 142 | LOG(WARNING) << "Unable to retrieve active user session."; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | AccountId account_id = user_session->user_info->account_id; |
| 147 | assistant_image_downloader_->Download(account_id, url, std::move(callback)); |
| 148 | } |
| 149 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 150 | void AssistantController::AddInteractionModelObserver( |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 151 | AssistantInteractionModelObserver* observer) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 152 | assistant_interaction_model_.AddObserver(observer); |
| 153 | } |
| 154 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 155 | void AssistantController::RemoveInteractionModelObserver( |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 156 | AssistantInteractionModelObserver* observer) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 157 | assistant_interaction_model_.RemoveObserver(observer); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 158 | } |
| 159 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 160 | void AssistantController::StartInteraction() { |
David Black | 46b99990 | 2018-05-16 19:59:02 | [diff] [blame] | 161 | if (!assistant_) { |
| 162 | ShowToast(kUnboundServiceToastId, kSomethingWentWrong); |
| 163 | return; |
| 164 | } |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 165 | OnInteractionStarted(); |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 166 | } |
| 167 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 168 | void AssistantController::StopInteraction() { |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 169 | assistant_interaction_model_.SetInteractionState(InteractionState::kInactive); |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 170 | } |
| 171 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 172 | void AssistantController::ToggleInteraction() { |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 173 | if (assistant_interaction_model_.interaction_state() == |
| 174 | InteractionState::kInactive) { |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 175 | StartInteraction(); |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 176 | } else { |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 177 | StopInteraction(); |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 181 | void AssistantController::OnInteractionStateChanged( |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 182 | InteractionState interaction_state) { |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 183 | if (interaction_state == InteractionState::kActive) |
| 184 | return; |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 185 | |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 186 | // When the user-facing interaction is dismissed, we instruct the service to |
| 187 | // terminate any listening, speaking, or query in flight. |
| 188 | DCHECK(assistant_); |
| 189 | assistant_->StopActiveInteraction(); |
| 190 | |
| 191 | assistant_interaction_model_.ClearInteraction(); |
| 192 | assistant_interaction_model_.SetInputModality(InputModality::kVoice); |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 193 | } |
| 194 | |
Qiang Xu | bd3dfbc7 | 2018-05-14 23:26:40 | [diff] [blame] | 195 | void AssistantController::OnHighlighterEnabledChanged( |
| 196 | HighlighterEnabledState state) { |
Qiang Xu | bd6d84e7 | 2018-05-09 01:31:51 | [diff] [blame] | 197 | assistant_interaction_model_.SetInputModality(InputModality::kStylus); |
Qiang Xu | bd3dfbc7 | 2018-05-14 23:26:40 | [diff] [blame] | 198 | if (state == HighlighterEnabledState::kEnabled) { |
| 199 | assistant_interaction_model_.SetInteractionState(InteractionState::kActive); |
| 200 | } else if (state == HighlighterEnabledState::kDisabledByUser) { |
| 201 | assistant_interaction_model_.SetInteractionState( |
| 202 | InteractionState::kInactive); |
| 203 | } |
Qiang Xu | bd6d84e7 | 2018-05-09 01:31:51 | [diff] [blame] | 204 | } |
| 205 | |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 206 | void AssistantController::OnInputModalityChanged(InputModality input_modality) { |
| 207 | if (input_modality == InputModality::kVoice) |
| 208 | return; |
| 209 | |
| 210 | // When switching to a non-voice input modality we instruct the underlying |
David Black | d192d24c | 2018-05-15 22:44:50 | [diff] [blame] | 211 | // service to terminate any listening, speaking, or in flight voice query. We |
| 212 | // do not do this when switching to voice input modality because initiation of |
| 213 | // a voice interaction will automatically interrupt any pre-existing activity. |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 214 | // Stopping the active interaction here for voice input modality would |
| 215 | // actually have the undesired effect of stopping the voice interaction. |
David Black | d192d24c | 2018-05-15 22:44:50 | [diff] [blame] | 216 | if (assistant_interaction_model_.query().type() == |
| 217 | AssistantQueryType::kVoice) { |
| 218 | DCHECK(assistant_); |
| 219 | assistant_->StopActiveInteraction(); |
| 220 | } |
David Black | 418376ae | 2018-05-15 18:20:10 | [diff] [blame] | 221 | } |
| 222 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 223 | void AssistantController::OnInteractionStarted() { |
David Black | 5318ec1 | 2018-05-02 01:54:32 | [diff] [blame] | 224 | assistant_interaction_model_.SetInteractionState(InteractionState::kActive); |
David Black | 4d28a35 | 2018-04-15 22:00:14 | [diff] [blame] | 225 | } |
| 226 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 227 | void AssistantController::OnInteractionFinished( |
David Black | ced6a61 | 2018-05-15 20:52:32 | [diff] [blame] | 228 | AssistantInteractionResolution resolution) { |
| 229 | // When a voice query is interrupted we do not receive any follow up speech |
| 230 | // recognition events but the mic is closed. |
| 231 | if (resolution == AssistantInteractionResolution::kInterruption) { |
| 232 | assistant_interaction_model_.SetMicState(MicState::kClosed); |
| 233 | } |
| 234 | } |
| 235 | |
David Black | 3d52759 | 2018-05-18 18:12:29 | [diff] [blame] | 236 | void AssistantController::OnCardPressed(const GURL& url) { |
| 237 | OnOpenUrlResponse(url); |
| 238 | } |
| 239 | |
David Black | ced6a61 | 2018-05-15 20:52:32 | [diff] [blame] | 240 | void AssistantController::OnDialogPlateActionPressed(const std::string& text) { |
| 241 | InputModality input_modality = assistant_interaction_model_.input_modality(); |
| 242 | |
| 243 | // When using keyboard input modality, pressing the dialog plate action is |
| 244 | // equivalent to a commit. |
| 245 | if (input_modality == InputModality::kKeyboard) { |
| 246 | OnDialogPlateContentsCommitted(text); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | DCHECK(assistant_); |
| 251 | |
| 252 | // It should not be possible to press the dialog plate action when not using |
| 253 | // keyboard or voice input modality. |
| 254 | DCHECK(input_modality == InputModality::kVoice); |
| 255 | |
| 256 | // When using voice input modality, pressing the dialog plate action will |
| 257 | // toggle the voice interaction state. |
| 258 | switch (assistant_interaction_model_.mic_state()) { |
| 259 | case MicState::kClosed: |
| 260 | assistant_->StartVoiceInteraction(); |
| 261 | break; |
| 262 | case MicState::kOpen: |
| 263 | assistant_->StopActiveInteraction(); |
| 264 | break; |
| 265 | } |
| 266 | } |
David Black | 4d28a35 | 2018-04-15 22:00:14 | [diff] [blame] | 267 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 268 | void AssistantController::OnDialogPlateContentsChanged( |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 269 | const std::string& text) { |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 270 | if (text.empty()) { |
| 271 | // Note: This does not open the mic. It only updates the input modality to |
| 272 | // voice so that we will show the mic icon in the UI. |
| 273 | assistant_interaction_model_.SetInputModality(InputModality::kVoice); |
| 274 | } else { |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 275 | assistant_interaction_model_.SetInputModality(InputModality::kKeyboard); |
| 276 | assistant_interaction_model_.SetMicState(MicState::kClosed); |
| 277 | } |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 278 | } |
| 279 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 280 | void AssistantController::OnDialogPlateContentsCommitted( |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 281 | const std::string& text) { |
David Black | ced6a61 | 2018-05-15 20:52:32 | [diff] [blame] | 282 | // TODO(dmblack): Handle an empty text query more gracefully by showing a |
| 283 | // helpful message to the user. Currently we just reset state and pretend as |
| 284 | // if nothing happened. |
| 285 | if (text.empty()) { |
| 286 | assistant_interaction_model_.ClearInteraction(); |
| 287 | assistant_interaction_model_.SetInputModality(InputModality::kVoice); |
| 288 | return; |
| 289 | } |
| 290 | |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 291 | assistant_interaction_model_.ClearInteraction(); |
David Black | 7a1b66ff | 2018-05-11 21:48:25 | [diff] [blame] | 292 | assistant_interaction_model_.SetQuery( |
| 293 | std::make_unique<AssistantTextQuery>(text)); |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 294 | |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 295 | // Note: This does not open the mic. It only updates the input modality to |
| 296 | // voice so that we will show the mic icon in the UI. |
| 297 | assistant_interaction_model_.SetInputModality(InputModality::kVoice); |
| 298 | |
David Black | b2df49c | 2018-05-03 23:52:36 | [diff] [blame] | 299 | DCHECK(assistant_); |
| 300 | assistant_->SendTextQuery(text); |
| 301 | } |
| 302 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 303 | void AssistantController::OnHtmlResponse(const std::string& response) { |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 304 | assistant_interaction_model_.AddUiElement( |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 305 | std::make_unique<AssistantCardElement>(response)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 306 | } |
| 307 | |
David Black | 59d2b8a | 2018-05-14 21:02:18 | [diff] [blame] | 308 | void AssistantController::OnSuggestionChipPressed(int id) { |
| 309 | const AssistantSuggestion* suggestion = |
| 310 | assistant_interaction_model_.GetSuggestionById(id); |
| 311 | |
| 312 | DCHECK(suggestion); |
| 313 | |
| 314 | // If the suggestion contains a non-empty action url, we will handle the |
| 315 | // suggestion chip pressed event by launching the action url in the browser. |
| 316 | if (!suggestion->action_url.is_empty()) { |
| 317 | OnOpenUrlResponse(suggestion->action_url); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // Otherwise, we will submit a simple text query using the suggestion text. |
| 322 | const std::string text = suggestion->text; |
| 323 | |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 324 | assistant_interaction_model_.ClearInteraction(); |
David Black | 7a1b66ff | 2018-05-11 21:48:25 | [diff] [blame] | 325 | assistant_interaction_model_.SetQuery( |
| 326 | std::make_unique<AssistantTextQuery>(text)); |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 327 | |
| 328 | DCHECK(assistant_); |
| 329 | assistant_->SendTextQuery(text); |
| 330 | } |
| 331 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 332 | void AssistantController::OnSuggestionsResponse( |
David Black | dc1996d3 | 2018-05-11 01:30:05 | [diff] [blame] | 333 | std::vector<AssistantSuggestionPtr> response) { |
| 334 | assistant_interaction_model_.AddSuggestions(std::move(response)); |
David Black | 46ad4b7 | 2018-04-03 00:34:02 | [diff] [blame] | 335 | } |
| 336 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 337 | void AssistantController::OnTextResponse(const std::string& response) { |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 338 | assistant_interaction_model_.AddUiElement( |
David Black | d72bae46 | 2018-04-28 00:47:14 | [diff] [blame] | 339 | std::make_unique<AssistantTextElement>(response)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 340 | } |
| 341 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 342 | void AssistantController::OnSpeechRecognitionStarted() { |
David Black | 71ab74d | 2018-04-12 06:52:31 | [diff] [blame] | 343 | assistant_interaction_model_.ClearInteraction(); |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 344 | assistant_interaction_model_.SetInputModality(InputModality::kVoice); |
| 345 | assistant_interaction_model_.SetMicState(MicState::kOpen); |
David Black | d192d24c | 2018-05-15 22:44:50 | [diff] [blame] | 346 | assistant_interaction_model_.SetQuery( |
| 347 | std::make_unique<AssistantVoiceQuery>()); |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 348 | } |
| 349 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 350 | void AssistantController::OnSpeechRecognitionIntermediateResult( |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 351 | const std::string& high_confidence_text, |
| 352 | const std::string& low_confidence_text) { |
David Black | 7a1b66ff | 2018-05-11 21:48:25 | [diff] [blame] | 353 | assistant_interaction_model_.SetQuery(std::make_unique<AssistantVoiceQuery>( |
| 354 | high_confidence_text, low_confidence_text)); |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 355 | } |
| 356 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 357 | void AssistantController::OnSpeechRecognitionEndOfUtterance() { |
David Black | 873ec755 | 2018-05-05 00:16:27 | [diff] [blame] | 358 | assistant_interaction_model_.SetMicState(MicState::kClosed); |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 359 | } |
| 360 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 361 | void AssistantController::OnSpeechRecognitionFinalResult( |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 362 | const std::string& final_result) { |
David Black | 7a1b66ff | 2018-05-11 21:48:25 | [diff] [blame] | 363 | assistant_interaction_model_.SetQuery( |
| 364 | std::make_unique<AssistantVoiceQuery>(final_result)); |
David Black | 89ac6f8 | 2018-04-03 17:38:12 | [diff] [blame] | 365 | } |
| 366 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 367 | void AssistantController::OnSpeechLevelUpdated(float speech_level) { |
Alan Lau | 7bd29ced | 2018-03-29 18:39:51 | [diff] [blame] | 368 | // TODO(dmblack): Handle. |
| 369 | NOTIMPLEMENTED(); |
| 370 | } |
| 371 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 372 | void AssistantController::OnOpenUrlResponse(const GURL& url) { |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 373 | Shell::Get()->shell_delegate()->OpenUrlFromArc(url); |
David Black | 990b0ac | 2018-05-01 18:08:08 | [diff] [blame] | 374 | StopInteraction(); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 375 | } |
| 376 | |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 377 | } // namespace ash |