-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
assistant_optin_flow.js
322 lines (288 loc) · 9.27 KB
/
assistant_optin_flow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Polymer element for displaying material design assistant
* ready screen.
*
*/
import '../components/common_styles/oobe_common_styles.css.js';
import './assistant_common_styles.m.js';
import './assistant_icon.m.js';
import './assistant_loading.js';
import './assistant_related_info.js';
import './assistant_voice_match.m.js';
import './assistant_value_prop.js';
import './setting_zippy.js';
import {loadTimeData} from '//resources/ash/common/load_time_data.m.js';
import {html, mixinBehaviors, PolymerElement} from '//resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {MultiStepBehavior, MultiStepBehaviorInterface} from '../components/behaviors/multi_step_behavior.js';
import {OobeDialogHostBehavior} from '../components/behaviors/oobe_dialog_host_behavior.js';
import {OobeI18nBehavior, OobeI18nBehaviorInterface} from '../components/behaviors/oobe_i18n_behavior.js';
import {BrowserProxyImpl} from './browser_proxy.m.js';
/**
* UI mode for the dialog.
* @enum {string}
*/
const AssistantUIState = {
LOADING: 'loading',
VALUE_PROP: 'value-prop',
RELATED_INFO: 'related-info',
VOICE_MATCH: 'voice-match',
};
/**
* @constructor
* @extends {PolymerElement}
*/
const AssistantOptInFlowBase = mixinBehaviors(
[OobeI18nBehavior, OobeDialogHostBehavior, MultiStepBehavior],
PolymerElement);
/**
* @polymer
*/
class AssistantOptInFlow extends AssistantOptInFlowBase {
static get is() {
return 'assistant-optin-flow-element';
}
static get template() {
return html`{__html_template__}`;
}
constructor() {
super();
this.UI_STEPS = AssistantUIState;
/** @private {?BrowserProxy} */
this.browserProxy_ = BrowserProxyImpl.getInstance();
}
/** @override */
attached() {
window.addEventListener('orientationchange', () => this.onWindowResized_());
window.addEventListener('resize', () => this.onWindowResized_());
}
/** @override */
detached() {
window.removeEventListener(
'orientationchange', () => this.onWindowResized_());
window.removeEventListener('resize', () => this.onWindowResized_());
}
/**
* Indicates the type of the opt-in flow.
*/
get FlowType() {
return {
// The whole consent flow.
CONSENT_FLOW: 0,
// The voice match enrollment flow.
SPEAKER_ID_ENROLLMENT: 1,
// The voice match retrain flow.
SPEAKER_ID_RETRAIN: 2,
};
}
defaultUIStep() {
return AssistantUIState.LOADING;
}
/**
* Signal from host to show the screen.
* @param {?string} type The type of the flow.
* @param {?string} captionBarHeight The height of the caption bar.
*/
onShow(type, captionBarHeight) {
captionBarHeight = captionBarHeight ? captionBarHeight + 'px' : '0px';
this.style.setProperty('--caption-bar-height', captionBarHeight);
this.onWindowResized_();
type = type ? type : this.FlowType.CONSENT_FLOW.toString();
const flowType = Number(type);
switch (flowType) {
case this.FlowType.CONSENT_FLOW:
case this.FlowType.SPEAKER_ID_ENROLLMENT:
case this.FlowType.SPEAKER_ID_RETRAIN:
this.flowType = flowType;
break;
default:
console.error('Invalid flow type, using default.');
this.flowType = this.FlowType.CONSENT_FLOW;
break;
}
this.boundShowLoadingScreen = () => this.showLoadingScreen();
this.boundOnScreenLoadingError = () => this.onScreenLoadingError();
this.boundOnScreenLoaded = () => this.onScreenLoaded();
this.$.loading.onBeforeShow();
this.$.loading.addEventListener('reload', () => this.onReload());
switch (this.flowType) {
case this.FlowType.SPEAKER_ID_ENROLLMENT:
case this.FlowType.SPEAKER_ID_RETRAIN:
this.$.voiceMatch.isFirstScreen = true;
this.showStep(AssistantUIState.VOICE_MATCH);
break;
default:
this.showStep(AssistantUIState.VALUE_PROP);
}
this.browserProxy_.initialized([this.flowType]);
}
/**
* Reloads localized strings.
* @param {!Object} data New dictionary with i18n values.
*/
reloadContent(data) {
this.voiceMatchEnforcedOff = data['voiceMatchEnforcedOff'];
this.shouldSkipVoiceMatch = data['shouldSkipVoiceMatch'];
this.voiceMatchDisabled = loadTimeData.getBoolean('voiceMatchDisabled');
data['flowType'] = this.flowType;
this.$.valueProp.reloadContent(data);
this.$.relatedInfo.reloadContent(data);
this.$.voiceMatch.reloadContent(data);
}
/**
* Add a setting zippy object in the corresponding screen.
* @param {string} type type of the setting zippy.
* @param {!Object} data String and url for the setting zippy.
*/
addSettingZippy(type, data) {
switch (type) {
case 'settings':
this.$.valueProp.addSettingZippy(data);
break;
default:
console.error('Undefined zippy data type: ' + type);
}
}
/**
* Show the next screen in the flow.
*/
showNextScreen() {
switch (this.currentStep) {
case AssistantUIState.VALUE_PROP:
this.showStep(AssistantUIState.RELATED_INFO);
break;
case AssistantUIState.RELATED_INFO:
if (this.voiceMatchEnforcedOff || this.voiceMatchDisabled ||
this.shouldSkipVoiceMatch) {
this.browserProxy_.flowFinished();
} else {
this.showStep(AssistantUIState.VOICE_MATCH);
}
break;
case AssistantUIState.VOICE_MATCH:
this.browserProxy_.flowFinished();
break;
default:
console.error('Undefined');
this.browserProxy_.dialogClose();
}
}
/**
* Called when the Voice match state is updated.
* @param {string} state the voice match state.
*/
onVoiceMatchUpdate(state) {
if (this.currentStep !== AssistantUIState.VOICE_MATCH) {
return;
}
switch (state) {
case 'listen':
this.$.voiceMatch.listenForHotword();
break;
case 'process':
this.$.voiceMatch.processingHotword();
break;
case 'done':
this.$.voiceMatch.voiceMatchDone();
break;
case 'failure':
this.onScreenLoadingError();
break;
default:
break;
}
}
/**
* Called to show the next settings when there are multiple unbundled
* activity control settings in the Value prop screen.
*/
onValuePropUpdate() {
if (this.currentStep !== AssistantUIState.VALUE_PROP) {
return;
}
this.$.valueProp.showNextStep();
}
/**
* Show the given step.
*
* @param {AssistantUIState} step The step to be shown.
*/
showStep(step) {
if (this.currentStep == step) {
return;
}
if (this.currentStep) {
this.applyToStepElements((screen) => {
screen.removeEventListener('loading', this.boundShowLoadingScreen);
screen.removeEventListener('error', this.boundOnScreenLoadingError);
screen.removeEventListener('loaded', this.boundOnScreenLoaded);
});
}
this.setUIStep(step);
this.currentStep = step;
this.applyToStepElements((screen) => {
screen.addEventListener('loading', this.boundShowLoadingScreen);
screen.addEventListener('error', this.boundOnScreenLoadingError);
screen.addEventListener('loaded', this.boundOnScreenLoaded);
screen.onShow();
});
}
/**
* Show the loading screen.
*/
showLoadingScreen() {
this.setUIStep(AssistantUIState.LOADING);
this.$.loading.onShow();
}
/**
* Called when the screen failed to load.
*/
onScreenLoadingError() {
this.setUIStep(AssistantUIState.LOADING);
this.$.loading.onErrorOccurred();
}
/**
* Called when all the content of current screen has been loaded.
*/
onScreenLoaded() {
this.setUIStep(this.currentStep);
this.$.loading.onPageLoaded();
}
/**
* Called when user request the screen to be reloaded.
*/
onReload() {
this.applyToStepElements((screen) => {
screen.reloadPage();
}, this.currentStep);
}
/**
* Called during initialization, when the window is resized, or the window's
* orientation is updated.
*/
onWindowResized_() {
// Dialog size and orientation value needs to be updated for in-session
// assistant dialog.
if (!document.documentElement.hasAttribute('screen')) {
document.documentElement.style.setProperty(
'--oobe-oobe-dialog-height-base', window.innerHeight + 'px');
document.documentElement.style.setProperty(
'--oobe-oobe-dialog-width-base', window.innerWidth + 'px');
if (window.innerWidth > window.innerHeight) {
document.documentElement.setAttribute('orientation', 'horizontal');
} else {
document.documentElement.setAttribute('orientation', 'vertical');
}
}
// In landscape mode, animation element should reside in subtitle slot which
// is shown at the bottom left of the screen. In portrait mode, animation
// element should reside in content slot which allows scrolling with the
// rest of the content.
const slot = window.innerWidth > window.innerHeight ? 'subtitle' : 'content';
this.$.valueProp.getAnimationContainer().slot = slot;
this.$.relatedInfo.getAnimationContainer().slot = slot;
}
}
customElements.define(AssistantOptInFlow.is, AssistantOptInFlow);