Add Modello web sample applications; version up
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / Modello_SDL / project / app / controller / sdl / Controller.js
1 /*
2  * Copyright (c) 2013, Ford Motor Company All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *  · Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  *  · Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  *  · Neither the name of the Ford Motor Company nor the names of its
12  * contributors may be used to endorse or promote products derived from this
13  * software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 /**
28  * @name SDL.SDLController
29  * @desc Main SDL Controller
30  * @category Controller
31  * @filesource app/controller/sdl/SDLController.js
32  * @version 1.0
33  */
34
35 SDL.SDLController = Em.Object.create( {
36
37     /**
38      * Current system context
39      *
40      * @type {String}
41      */
42     sysContext: function() {
43
44         if( SDL.VRPopUp.VRActive ){
45             return 'VRSESSION';
46         }
47
48         if( SDL.AlertPopUp.active ){
49             return 'ALERT';
50         }
51
52         if( SDL.TBTClientStateView.active || SDL.VehicleInfo.active || SDL.DriverDistraction.active ){
53             return 'HMI_OBSCURED';
54         }
55
56         if( SDL.OptionsView.active ){
57             return 'MENU';
58         }
59
60         if( SDL.States.info.nonMedia.active || SDL.States.media.sdlmedia.active ){
61             return 'MAIN';
62         }else{
63             return 'MENU';
64         }
65
66     }.property( 'SDL.DriverDistraction.active', 'SDL.OptionsView.active', 'SDL.VRPopUp.VRActive', 'SDL.AlertPopUp.active', 'SDL.TBTClientStateView.active',
67                     'SDL.VehicleInfo.active', 'SDL.States.info.nonMedia.active', 'SDL.States.media.sdlmedia.active' ),
68
69     /**
70      * List of SDL application models
71      *
72      * @type object
73      */
74     applicationModels: {
75         0: SDL.SDLMediaModel,
76         1: SDL.SDLNonMediaModel
77     },
78
79     /**
80      * Default action for SoftButtons: closes window, popUp or clears
81      * applications screen
82      *
83      * @param {Object}
84      */
85     defaultActionSoftButton: function( element ) {
86         switch( element.groupName ){
87
88             case "AlertPopUp": {
89                 SDL.AlertPopUp.deactivate( true );
90                 break;
91             }
92             case "ScrollableMessage": {
93                 SDL.ScrollableMessage.deactivate();
94                 break;
95             }
96             case "TurnByTurnView": {
97                 SDL.TurnByTurnView.deactivate();
98                 break;
99             }
100             case "NonMediaView": {
101                 SDL.SDLController.getApplicationModel( element.appId ).clearAppOverLay();
102                 break;
103             }
104             case "MediaView": {
105                 SDL.SDLController.getApplicationModel( element.appId ).clearAppOverLay();
106                 break;
107             }
108
109         }
110     },
111
112     /**
113      * Action to show Voice Recognition PopUp
114      */
115     activateVRPopUp: function() {
116         SDL.VRPopUp.activateVRPopUp();
117     },
118
119     /**
120      * Action for SoftButtons that closes popUp or window and opens applications
121      * screen
122      *
123      * @param {Object}
124      */
125     stealFocusSoftButton: function( element ) {
126         switch( element.groupName ){
127
128             case "AlertPopUp": {
129                 SDL.AlertPopUp.deactivate();
130                 SDL.SDLController.getApplicationModel( element.appId ).turnOnSDL();
131                 break;
132             }
133             case "ScrollableMessage": {
134                 SDL.ScrollableMessage.deactivate();
135                 SDL.SDLController.getApplicationModel( element.appId ).turnOnSDL();
136                 break;
137             }
138
139         }
140     },
141
142     /**
143      * Action for SoftButtons that clears popUps timer and it become visible all
144      * the time until user user closes it
145      *
146      * @param {Object}
147      */
148     keepContextSoftButton: function( element ) {
149         switch( element.groupName ){
150
151             case "AlertPopUp": {
152                 SDL.AlertPopUp.set( 'timer', 0 );
153                 break;
154             }
155             case "ScrollableMessage": {
156                 SDL.ScrollableMessage.set( 'timer', 0 );
157                 break;
158             }
159
160         }
161     },
162
163     /**
164      * Method to close AlertMeneuverPopUp view
165      */
166     closeAlertMeneuverPopUp: function() {
167         SDL.AlertManeuverPopUp.set( 'activate', false );
168     },
169
170     /**
171      * Method to open Turn List view from TBT
172      *
173      * @param {Number} appId AppId of activated sdl application
174      */
175     tbtTurnList: function( appId ) {
176         SDL.TBTTurnList.activate( appId );
177     },
178
179     /**
180      * Method to sent notification with selected state of TBT Client State
181      *
182      * @param {String}
183      */
184     tbtClientStateSeleced: function( state ) {
185         FFW.UI.onTBTClientState( state, SDL.SDLAppController.model.appId );
186     },
187
188     /**
189      * Method to sent notification ABORTED for PerformInteractionChoise
190      */
191     interactionChoiseCloseResponse: function( result, performInteractionRequestId ) {
192         FFW.UI.interactionResponse( result, performInteractionRequestId );
193     },
194
195     /**
196      * Method to sent notification for Alert
197      *
198      * @param {String} result
199      * @param {Number} alertRequestId
200      */
201     alertResponse: function( result, alertRequestId ) {
202         FFW.UI.sendUIResult( result, alertRequestId, 'UI.Alert' );
203     },
204
205     /**
206      * Method to sent notification for Scrollable Message
207      *
208      * @param {String} result
209      * @param {Number} messageRequestId
210      */
211     scrollableMessageResponse: function( result, messageRequestId ) {
212         FFW.UI.sendUIResult( result, messageRequestId, 'UI.ScrollableMessage' );
213     },
214
215     /**
216      * Method to sent notification for Slider
217      *
218      * @param {String} result
219      * @param {Number} sliderRequestId
220      */
221     sliderResponse: function( result, sliderRequestId ) {
222         FFW.UI.sendUIResult( result, sliderRequestId, 'UI.Slider' );
223     },
224
225     /**
226      * Method to call performAudioPassThruResponse with Result code parameters
227      *
228      * @param {Object} element Button object
229      */
230     callPerformAudioPassThruPopUpResponse: function( element ) {
231         this.performAudioPassThruResponse( element.responseResult );
232     },
233
234     /**
235      * Method close PerformAudioPassThruPopUp and call response from UIRPC back
236      * to SDLCore
237      *
238      * @param {String} result Result code
239      */
240     performAudioPassThruResponse: function( result ) {
241         SDL.SDLModel.set( 'AudioPassThruState', false );
242         FFW.UI.sendUIResult( result, FFW.UI.performAudioPassThruRequestId, "UI.PerformAudioPassThru" );
243     },
244
245     /**
246      * Method to set language for UI component with parameters sent from SDLCore
247      * to UIRPC
248      */
249     onLanguageChangeUI: function() {
250         FFW.UI.OnLanguageChange( SDL.SDLModel.hmiUILanguage );
251     }.observes( 'SDL.SDLModel.hmiUILanguage' ),
252
253     /**
254      * Method to set language for TTS and VR components with parameters sent
255      * from SDLCore to UIRPC
256      */
257     onLanguageChangeTTSVR: function() {
258         FFW.TTS.OnLanguageChange( SDL.SDLModel.hmiTTSVRLanguage );
259         FFW.VR.OnLanguageChange( SDL.SDLModel.hmiTTSVRLanguage );
260     }.observes( 'SDL.SDLModel.hmiTTSVRLanguage' ),
261
262     /**
263      * Register application
264      *
265      * @param {Object} params
266      * @param {Number} applicationType
267      */
268     registerApplication: function( params, applicationType ) {
269
270         SDL.SDLModel.get( 'registeredApps' ).pushObject( SDL.SDLController.applicationModels[applicationType].create( {
271             appId: params.appId,
272             appName: params.appName,
273             deviceName: params.deviceName
274         } ) );
275
276     },
277
278     /**
279      * Unregister application
280      *
281      * @param {Number} appId
282      */
283     unregisterApplication: function( appId ) {
284
285         this.getApplicationModel( appId ).onDeleteApplication( appId );
286
287         this.getApplicationModel( appId ).set( 'active', false );
288
289         var index = SDL.SDLModel.registeredApps.indexOf( SDL.SDLModel.registeredApps.filterProperty( 'appId', appId )[0] );
290
291         SDL.SDLModel.registeredApps.replace( index, 1 );
292
293         this.set( 'model', null );
294
295     },
296
297     /**
298      * SDL Driver Distraction ON/OFF switcher
299      */
300     selectDriverDistraction: function() {
301         if( SDL.SDLModel.driverDistractionState ){
302             FFW.UI.onDriverDistraction( "DD_ON" );
303         }else{
304             FFW.UI.onDriverDistraction( "DD_OFF" );
305         }
306     }.observes( 'SDL.SDLModel.driverDistractionState' ),
307
308     /**
309      * SDL Protocol Version 2 ON/OFF switcher
310      */
311     selectProtocolVersion: function() {
312         if( SDL.SDLModel.protocolVersion2State ){
313             FFW.BasicCommunication.OnVersionChanged( 2 );
314         }else{
315             FFW.BasicCommunication.OnVersionChanged( 1 );
316         }
317     }.observes( 'SDL.SDLModel.protocolVersion2State' ),
318
319     /**
320      * Get application model
321      *
322      * @param {Number}
323      */
324     getApplicationModel: function( applicationId ) {
325         return SDL.SDLModel.registeredApps.filterProperty( 'appId', applicationId )[0];
326     },
327
328     /**
329      * Function returns ChangeDeviceView back to previous state
330      */
331     turnChangeDeviceViewBack: function() {
332         SDL.States.goToStates( 'info.apps' );
333     },
334
335     /**
336      * Enter screen vith list of devices application model
337      */
338     onGetDeviceList: function() {
339         SDL.States.goToStates( 'info.devicelist' );
340         FFW.BasicCommunication.getDeviceList();
341     },
342
343     /**
344      * Send notification if device was choosed
345      *
346      * @param element: SDL.Button
347      */
348     onDeviceChoosed: function( element ) {
349         FFW.UI.OnDeviceChosen( element.deviceName );
350         this.turnChangeDeviceViewBack();
351     },
352
353     /**
354      * Method creates list of Application ID's Then call HMI method for display
355      * a list of Applications
356      *
357      * @param {Object}
358      */
359     onGetAppList: function( appList ) {
360         SDL.SDLModel.onGetAppList( appList );
361     },
362
363     /**
364      * Method call's request to get list of applications
365      */
366     findNewApps: function() {
367         FFW.BasicCommunication.getAppList();
368     },
369
370     /**
371      * Method activates selected registered application
372      *
373      * @param {Object}
374      */
375     onActivateSDLApp: function( element ) {
376         this.getApplicationModel( element.appId ).turnOnSDL();
377     },
378
379     /**
380      * Method sent custom softButtons pressed and event status to RPC
381      *
382      * @param {Object}
383      */
384     onSoftButtonActionUpCustom: function( element ) {
385         if( element.time > 0 ){
386             FFW.Buttons.buttonEventCustom( "CUSTOM_BUTTON", "BUTTONUP", element.softButtonID );
387         }else{
388             FFW.Buttons.buttonEventCustom( "CUSTOM_BUTTON", "BUTTONUP", element.softButtonID );
389             FFW.Buttons.buttonPressedCustom( "CUSTOM_BUTTON", "SHORT", element.softButtonID );
390         }
391         clearTimeout( element.timer );
392         element.time = 0;
393     },
394
395     /**
396      * Method sent custom softButtons pressed and event status to RPC
397      *
398      * @param {Object}
399      */
400     onSoftButtonActionDownCustom: function( element ) {
401         FFW.Buttons.buttonEventCustom( "CUSTOM_BUTTON", "BUTTONDOWN", element.softButtonID );
402         element.time = 0;
403         element.timer = setTimeout( function() {
404             FFW.Buttons.buttonPressedCustom( "CUSTOM_BUTTON", "LONG", element.softButtonID );
405             element.time++;
406         }, 2000 );
407     },
408
409     /**
410      * Method sent softButtons pressed and event status to RPC
411      *
412      * @param {String}
413      * @param {Object}
414      */
415     onSoftButtonActionUp: function( element ) {
416         if( element.time > 0 ){
417             FFW.Buttons.buttonEvent( element.presetName, "BUTTONUP" );
418         }else{
419             FFW.Buttons.buttonEvent( element.presetName, "BUTTONUP" );
420             FFW.Buttons.buttonPressed( element.presetName, "SHORT" );
421         }
422         clearTimeout( element.timer );
423         element.time = 0;
424     },
425
426     /**
427      * Method sent softButtons Ok pressed and event status to RPC
428      *
429      * @param {String}
430      */
431     onSoftButtonOkActionDown: function( name ) {
432         FFW.Buttons.buttonEvent( name, "BUTTONDOWN" );
433     },
434
435     /**
436      * Method sent softButton OK pressed and event status to RPC
437      *
438      * @param {String}
439      */
440     onSoftButtonOkActionUp: function( name ) {
441         FFW.Buttons.buttonEvent( name, "BUTTONUP" );
442         FFW.Buttons.buttonPressed( name, "SHORT" );
443         SDL.SDLAppController.model.set( 'isPlaying', !SDL.SDLAppController.model.isPlaying );
444     },
445
446     /**
447      * Method sent softButtons pressed and event status to RPC
448      *
449      * @param {String}
450      * @param {Object}
451      */
452     onSoftButtonActionDown: function( element ) {
453         FFW.Buttons.buttonEvent( element.presetName, "BUTTONDOWN" );
454         element.time = 0;
455         element.timer = setTimeout( function() {
456             FFW.Buttons.buttonPressed( element.presetName, "LONG" );
457             element.time++;
458         }, 2000 );
459     },
460
461     /**
462      * Send system context
463      */
464     onSystemContextChange: function() {
465         FFW.UI.OnSystemContext( this.get( 'sysContext' ) );
466     }
467 } );