OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function() { | 5 (function() { |
6 | 6 |
7 // Correspond to steps in the hotword opt-in flow. | 7 // Correspond to steps in the hotword opt-in flow. |
8 /** @const */ var START = 'start-container'; | 8 /** @const */ var START = 'start-container'; |
9 /** @const */ var AUDIO_HISTORY = 'audio-history-container'; | 9 /** @const */ var AUDIO_HISTORY = 'audio-history-container'; |
10 /** @const */ var SPEECH_TRAINING = 'speech-training-container'; | 10 /** @const */ var SPEECH_TRAINING = 'speech-training-container'; |
11 /** @const */ var FINISH = 'finish-container'; | 11 /** @const */ var FINISH = 'finish-container'; |
12 | 12 |
13 /** | 13 /** |
14 * These flows correspond to the three LaunchModes as defined in | 14 * These flows correspond to the three LaunchModes as defined in |
15 * chrome/browser/search/hotword_service.h and should be kept in sync | 15 * chrome/browser/search/hotword_service.h and should be kept in sync |
16 * with them. | 16 * with them. |
17 * @const | 17 * @const |
18 */ | 18 */ |
19 var FLOWS = [ | 19 var FLOWS = [ |
20 [START, SPEECH_TRAINING, FINISH], | 20 [START, SPEECH_TRAINING, FINISH], |
21 [START, AUDIO_HISTORY, SPEECH_TRAINING, FINISH], | 21 [START, AUDIO_HISTORY, SPEECH_TRAINING, FINISH], [SPEECH_TRAINING, FINISH] |
22 [SPEECH_TRAINING, FINISH] | |
23 ]; | 22 ]; |
24 | 23 |
25 /** | 24 /** |
26 * The launch mode. This enum needs to be kept in sync with that of | 25 * The launch mode. This enum needs to be kept in sync with that of |
27 * the same name in hotword_service.h. | 26 * the same name in hotword_service.h. |
28 * @enum {number} | 27 * @enum {number} |
29 */ | 28 */ |
30 var LaunchMode = { | 29 var LaunchMode = {HOTWORD_ONLY: 0, HOTWORD_AND_AUDIO_HISTORY: 1, RETRAIN: 2}; |
31 HOTWORD_ONLY: 0, | |
32 HOTWORD_AND_AUDIO_HISTORY: 1, | |
33 RETRAIN: 2 | |
34 }; | |
35 | 30 |
36 /** | 31 /** |
37 * The training state. | 32 * The training state. |
38 * @enum {string} | 33 * @enum {string} |
39 */ | 34 */ |
40 var TrainingState = { | 35 var TrainingState = { |
41 RESET: 'reset', | 36 RESET: 'reset', |
42 TIMEOUT: 'timeout', | 37 TIMEOUT: 'timeout', |
43 ERROR: 'error', | 38 ERROR: 'error', |
44 }; | 39 }; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 * Listener for the speakerModelSaved event. | 94 * Listener for the speakerModelSaved event. |
100 * @private {Function} | 95 * @private {Function} |
101 */ | 96 */ |
102 this.speakerModelFinalizedListener_ = | 97 this.speakerModelFinalizedListener_ = |
103 this.onSpeakerModelFinalized_.bind(this); | 98 this.onSpeakerModelFinalized_.bind(this); |
104 | 99 |
105 /** | 100 /** |
106 * Listener for the hotword trigger event. | 101 * Listener for the hotword trigger event. |
107 * @private {Function} | 102 * @private {Function} |
108 */ | 103 */ |
109 this.hotwordTriggerListener_ = | 104 this.hotwordTriggerListener_ = this.handleHotwordTrigger_.bind(this); |
110 this.handleHotwordTrigger_.bind(this); | |
111 | 105 |
112 // Listen for the user locking the screen. | 106 // Listen for the user locking the screen. |
113 chrome.idle.onStateChanged.addListener( | 107 chrome.idle.onStateChanged.addListener( |
114 this.handleIdleStateChanged_.bind(this)); | 108 this.handleIdleStateChanged_.bind(this)); |
115 | 109 |
116 // Listen for hotword settings changes. This used to detect when the user | 110 // Listen for hotword settings changes. This used to detect when the user |
117 // switches to a different profile. | 111 // switches to a different profile. |
118 if (chrome.hotwordPrivate.onEnabledChanged) { | 112 if (chrome.hotwordPrivate.onEnabledChanged) { |
119 chrome.hotwordPrivate.onEnabledChanged.addListener( | 113 chrome.hotwordPrivate.onEnabledChanged.addListener( |
120 this.handleEnabledChanged_.bind(this)); | 114 this.handleEnabledChanged_.bind(this)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 160 |
167 /** | 161 /** |
168 * Stops the training process. | 162 * Stops the training process. |
169 */ | 163 */ |
170 Flow.prototype.stopTraining = function() { | 164 Flow.prototype.stopTraining = function() { |
171 if (!this.training_) | 165 if (!this.training_) |
172 return; | 166 return; |
173 | 167 |
174 this.training_ = false; | 168 this.training_ = false; |
175 if (chrome.hotwordPrivate.onHotwordTriggered) { | 169 if (chrome.hotwordPrivate.onHotwordTriggered) { |
176 chrome.hotwordPrivate.onHotwordTriggered. | 170 chrome.hotwordPrivate.onHotwordTriggered.removeListener( |
177 removeListener(this.hotwordTriggerListener_); | 171 this.hotwordTriggerListener_); |
178 } | 172 } |
179 if (chrome.hotwordPrivate.stopTraining) | 173 if (chrome.hotwordPrivate.stopTraining) |
180 chrome.hotwordPrivate.stopTraining(); | 174 chrome.hotwordPrivate.stopTraining(); |
181 }; | 175 }; |
182 | 176 |
183 /** | 177 /** |
184 * Attempts to enable audio history for the signed-in account. | 178 * Attempts to enable audio history for the signed-in account. |
185 */ | 179 */ |
186 Flow.prototype.enableAudioHistory = function() { | 180 Flow.prototype.enableAudioHistory = function() { |
187 // Update UI | 181 // Update UI |
(...skipping 18 matching lines...) Expand all Loading... |
206 Flow.prototype.handleAudioHistoryError_ = function() { | 200 Flow.prototype.handleAudioHistoryError_ = function() { |
207 $('audio-history-agree').disabled = false; | 201 $('audio-history-agree').disabled = false; |
208 $('audio-history-cancel').disabled = false; | 202 $('audio-history-cancel').disabled = false; |
209 | 203 |
210 $('audio-history-wait').hidden = true; | 204 $('audio-history-wait').hidden = true; |
211 $('audio-history-error').hidden = false; | 205 $('audio-history-error').hidden = false; |
212 | 206 |
213 // Set a timeout before focusing the Enable button so that screenreaders | 207 // Set a timeout before focusing the Enable button so that screenreaders |
214 // have time to announce the error first. | 208 // have time to announce the error first. |
215 this.setTimeout_(function() { | 209 this.setTimeout_(function() { |
216 $('audio-history-agree').focus(); | 210 $('audio-history-agree').focus(); |
217 }.bind(this), 50); | 211 }.bind(this), 50); |
218 }; | 212 }; |
219 | 213 |
220 /** | 214 /** |
221 * Callback for when an audio history request completes. | 215 * Callback for when an audio history request completes. |
222 * @param {chrome.hotwordPrivate.AudioHistoryState} state The audio history | 216 * @param {chrome.hotwordPrivate.AudioHistoryState} state The audio history |
223 * request state. | 217 * request state. |
224 * @private | 218 * @private |
225 */ | 219 */ |
226 Flow.prototype.onAudioHistoryRequestCompleted_ = function(state) { | 220 Flow.prototype.onAudioHistoryRequestCompleted_ = function(state) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 this.stopTraining(); | 254 this.stopTraining(); |
261 this.setTimeout_(this.finishFlow_.bind(this), 2000); | 255 this.setTimeout_(this.finishFlow_.bind(this), 2000); |
262 }; | 256 }; |
263 | 257 |
264 /** | 258 /** |
265 * Completes the training process. | 259 * Completes the training process. |
266 * @private | 260 * @private |
267 */ | 261 */ |
268 Flow.prototype.finishFlow_ = function() { | 262 Flow.prototype.finishFlow_ = function() { |
269 if (chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled) { | 263 if (chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled) { |
270 chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled(true, | 264 chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled( |
271 this.advanceStep.bind(this)); | 265 true, this.advanceStep.bind(this)); |
272 } | 266 } |
273 }; | 267 }; |
274 | 268 |
275 /** | 269 /** |
276 * Handles a user clicking on the retry button. | 270 * Handles a user clicking on the retry button. |
277 */ | 271 */ |
278 Flow.prototype.handleRetry = function() { | 272 Flow.prototype.handleRetry = function() { |
279 if (!(this.trainingState_ == TrainingState.TIMEOUT || | 273 if (!(this.trainingState_ == TrainingState.TIMEOUT || |
280 this.trainingState_ == TrainingState.ERROR)) | 274 this.trainingState_ == TrainingState.ERROR)) |
281 return; | 275 return; |
282 | 276 |
283 this.startTraining(); | 277 this.startTraining(); |
284 this.updateTrainingState_(TrainingState.RESET); | 278 this.updateTrainingState_(TrainingState.RESET); |
285 }; | 279 }; |
286 | 280 |
287 // ---- private methods: | 281 // ---- private methods: |
288 | 282 |
289 /** | 283 /** |
290 * Completes the training process. | 284 * Completes the training process. |
(...skipping 24 matching lines...) Expand all Loading... |
315 * @return {Object} The current training step, its index, and an array of | 309 * @return {Object} The current training step, its index, and an array of |
316 * all training steps. Any of these can be undefined. | 310 * all training steps. Any of these can be undefined. |
317 * @private | 311 * @private |
318 */ | 312 */ |
319 Flow.prototype.getCurrentTrainingStep_ = function(curStepClassName) { | 313 Flow.prototype.getCurrentTrainingStep_ = function(curStepClassName) { |
320 var steps = | 314 var steps = |
321 $(this.trainingPagePrefix_ + '-training').querySelectorAll('.train'); | 315 $(this.trainingPagePrefix_ + '-training').querySelectorAll('.train'); |
322 var curStep = | 316 var curStep = |
323 $(this.trainingPagePrefix_ + '-training').querySelector('.listening'); | 317 $(this.trainingPagePrefix_ + '-training').querySelector('.listening'); |
324 | 318 |
325 return {current: curStep, | 319 return { |
326 index: Array.prototype.indexOf.call(steps, curStep), | 320 current: curStep, |
327 steps: steps}; | 321 index: Array.prototype.indexOf.call(steps, curStep), |
| 322 steps: steps |
| 323 }; |
328 }; | 324 }; |
329 | 325 |
330 /** | 326 /** |
331 * Updates the training state. | 327 * Updates the training state. |
332 * @param {TrainingState} state The training state. | 328 * @param {TrainingState} state The training state. |
333 * @private | 329 * @private |
334 */ | 330 */ |
335 Flow.prototype.updateTrainingState_ = function(state) { | 331 Flow.prototype.updateTrainingState_ = function(state) { |
336 this.trainingState_ = state; | 332 this.trainingState_ = state; |
337 this.updateErrorUI_(); | 333 this.updateErrorUI_(); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 }; | 520 }; |
525 | 521 |
526 /** | 522 /** |
527 * Gets and starts the appropriate flow for the launch mode. | 523 * Gets and starts the appropriate flow for the launch mode. |
528 * @param {chrome.hotwordPrivate.LaunchState} state Launch state of the | 524 * @param {chrome.hotwordPrivate.LaunchState} state Launch state of the |
529 * Hotword Audio Verification App. | 525 * Hotword Audio Verification App. |
530 * @private | 526 * @private |
531 */ | 527 */ |
532 Flow.prototype.startFlowForMode_ = function(state) { | 528 Flow.prototype.startFlowForMode_ = function(state) { |
533 this.launchMode_ = state.launchMode; | 529 this.launchMode_ = state.launchMode; |
534 assert(state.launchMode >= 0 && state.launchMode < FLOWS.length, | 530 assert( |
535 'Invalid Launch Mode.'); | 531 state.launchMode >= 0 && state.launchMode < FLOWS.length, |
| 532 'Invalid Launch Mode.'); |
536 this.currentFlow_ = FLOWS[state.launchMode]; | 533 this.currentFlow_ = FLOWS[state.launchMode]; |
537 if (state.launchMode == LaunchMode.HOTWORD_ONLY) { | 534 if (state.launchMode == LaunchMode.HOTWORD_ONLY) { |
538 $('intro-description-audio-history-enabled').hidden = false; | 535 $('intro-description-audio-history-enabled').hidden = false; |
539 } else if (state.launchMode == LaunchMode.HOTWORD_AND_AUDIO_HISTORY) { | 536 } else if (state.launchMode == LaunchMode.HOTWORD_AND_AUDIO_HISTORY) { |
540 $('intro-description').hidden = false; | 537 $('intro-description').hidden = false; |
541 } | 538 } |
542 | 539 |
543 this.advanceStep(); | 540 this.advanceStep(); |
544 }; | 541 }; |
545 | 542 |
(...skipping 14 matching lines...) Expand all Loading... |
560 previousStep = this.currentFlow_[this.currentStepIndex_ - 1]; | 557 previousStep = this.currentFlow_[this.currentStepIndex_ - 1]; |
561 | 558 |
562 if (previousStep) | 559 if (previousStep) |
563 document.getElementById(previousStep).hidden = true; | 560 document.getElementById(previousStep).hidden = true; |
564 | 561 |
565 chrome.app.window.current().show(); | 562 chrome.app.window.current().show(); |
566 }; | 563 }; |
567 | 564 |
568 window.Flow = Flow; | 565 window.Flow = Flow; |
569 })(); | 566 })(); |
OLD | NEW |