8d1ac8be93c453a5a96d3dd716468b2ab6cc4292
[profile/ivi/SettingsApp.git] / js / panel-connman.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 var connmanScanInProgress = false;
10
11 function connmanPanelInit() {
12
13     /* Connman Settings Panel */
14     $('#page_connman').on('pageshow', function(event, data) {
15         if (data.prevPage.attr('id') === 'page_connman_service') return;
16
17         wsAPI.subscribeEvents(connmanEventReceived);
18
19         console.log('Get all the available technologies');
20         $('#connman_technologies').html('');
21         settings.connman.getTechnologies(function(technologies) {
22             for (var i = 0; i < technologies.length; i++) {
23                 var technology = technologies[i];
24                 console.log('Connman technology found: ' + technology.prop.Type);
25                 if (technology.prop.Type === 'bluetooth') {
26                     /* Do not show bluetooth */
27                     continue;
28                 }
29                 if (technology.prop.Powered === undefined) {
30                     (function(t) {
31                         t.getPowered(function(is_powered) {
32                             t.prop.Powered = is_powered;
33                             connmanConstructTechnologyElement(t);
34                         }, function(e) {
35                             t.prop.Powered = false;
36                             connmanConstructTechnologyElement(t);
37                         });
38                     })(technology);
39                 } else {
40                     connmanConstructTechnologyElement(technology);
41                 }
42             }
43         }, function(e) {
44             showMsg('Error', e);
45         });
46     });
47
48     $('#button_connman_refresh').on('click', function() {
49         /* connman only supports wifi scan */
50         if ($('#toggle_connman_wifi').val() === 'on') {
51             connmanScan('/net/connman/technology/wifi');
52         } else {
53             connmanSync(null, function(e) {
54                 showMsg('Error', e);
55             });
56         }
57     });
58
59     $('#button_connman_wifi_add').on('click', function() {
60         if (connmanScanInProgress) return;
61
62         console.log('Enter add WiFi page');
63         $.mobile.changePage('#page_wifi_add');
64     });
65
66     /* Connman tethering page */
67     $('#page_connman_tethering').on('pageshow', function(event, data) {
68         connmanTetheringPageReload();
69     });
70
71     /* Connman service detail page */
72     $('#page_connman_service').on('pageshow', function(event, data) {
73         var service_id = localStorage.getItem('connman_service_id');
74         if (service_id == undefined) return;
75         var service = $(jqId(service_id)).data('service-object');
76         connmanConstructServicePanel(service);
77     });
78
79     /* WiFi add new network page */
80     $('#page_wifi_add').on('pagebeforeshow', function(event, data) {
81         $('#input_wifi_add_ssid').val('');
82         $('#input_wifi_add_password').val('');
83         $('#radio_wifi_none').prop('checked', true);
84         $('input[name=radio_wifi_security]').checkboxradio('refresh');
85         $('#input_wifi_add_password').textinput('disable');
86     });
87
88     $('#button_wifi_add').on('click', function() {
89         var name = $('#input_wifi_add_ssid').val();
90         var security = $('input[name=radio_wifi_security]:checked').val();
91         var passcode = $('#input_wifi_add_password').val();
92
93         if (name === '') {
94             showMsg('Error', 'Enter SSID');
95             return;
96         }
97
98         if (security !== 'none' && passcode === '') {
99             showMsg('Error', 'Enter passphrase');
100             return;
101         }
102
103         showSpinner(false, 'Adding...');
104         connmanGetHiddenServicePath(security, function(service_path) {
105             var service = new settings.connman.ConnmanService('', {
106                 'Name': name,
107                 'EncryptionMode': security
108             });
109             service.connect(passcode, function() {
110                 /* success */
111                 connmanSync(function() {
112                     hideSpinner();
113                     console.log('Go back to Connman page');
114                     $.mobile.changePage('#page_connman');
115                 }, function(e) {
116                     showMsg('Error', e);
117                 });
118             }, function(e) {
119                 /* error */
120                 hideSpinner();
121                 showMsg('Error', 'Failed to add network: ' + e);
122             });
123         }, function(e) {
124             hideSpinner();
125             showMsg('Error', e);
126         });
127     });
128
129     $('[name="radio_wifi_security"]').on('change', function(event, data) {
130         var security = $('input[name=radio_wifi_security]:checked').val();
131
132         if (security === 'none') {
133             $('#input_wifi_add_password').textinput('disable');
134             $('#input_wifi_add_password').val('');
135         } else {
136             $('#input_wifi_add_password').textinput('enable');
137         }
138     });
139
140     $('#button_wifi_connect').on('click', function() {
141         var service_id = localStorage.getItem('connman_service_id');
142         if (service_id == undefined) return;
143         var service = $(jqId(service_id)).data('service-object');
144         var passcode = $('#input_wifi_connect_passphrase').val();
145         connmanConnectToService(service, passcode, function() {
146             console.log('Successfully connected to service: ' + service.prop.Name);
147             if ($.mobile.activePage.attr('id') !== 'page_connman') {
148                 console.log('Go back to Connman page');
149                 $.mobile.changePage('#page_connman');
150             }
151         }, function(e) {
152             showMsg('Error', 'Connect service failed: ' + e);
153         });
154     });
155 }
156
157 function connmanEventReceived(event) {
158     if (event.type === WS_EVENT_TYPE.CONNMAN) {
159         if ($.mobile.activePage.attr('id') !== 'page_connman' && $.mobile.activePage.attr('id') !== 'page_connman_service') {
160             return;
161         }
162
163         if (event.name === 'ServicesChanged') {
164             connmanHandleServicesChanged(event.id, event.value);
165         } else if (event.name === 'PropertyChanged') {
166             connmanHandlePropertyChanged(event.id, event.value);
167         } else {
168             console.log('Unsupported event received: ' + event.name);
169         }
170     }
171 }
172
173 function connmanHandleServicesChanged(object_path, services) {
174     var servicesChanged = services[0];
175     var servicesRemoved = services[1];
176
177     console.log(servicesChanged.length + ' services changed');
178     for (var i = 0; i < servicesChanged.length; i++) {
179         if (servicesChanged[i][0] === undefined) {
180             console.log('Invalid parameters, missing object path');
181             continue;
182         }
183
184         var service = $(jqId(servicesChanged[i][0])).data('service-object');
185
186         if (service == null) {
187             console.log('could not find service object ' + servicesChanged[i][0]);
188             continue;
189         }
190
191         for (var prop in servicesChanged[i][1]) {
192             if (servicesChanged[i][1].hasOwnProperty(prop)) {
193                 service.prop.prop = servicesChanged[i][1].prop;
194                 connmanUpdateService(service);
195                 console.log('Service ' + service.prop.Name + ' updated: ' + prop);
196             }
197         }
198     }
199
200     console.log(servicesRemoved.length + ' services removed');
201     for (var i = 0; i < servicesRemoved.length; i++) {
202         if (servicesRemoved[i] === undefined) {
203             console.log('Invalid parameters, missing object path');
204             continue;
205         }
206
207         connmanRemoveFromAvailableList(servicesRemoved[i]);
208     }
209 }
210
211 function connmanHandlePropertyChanged(id, property) {
212     if (property[0] === 'Powered') {
213         var index = id.lastIndexOf('/');
214         var technology = id.substring(index + 1);
215         if (property[1] === true) {
216             connmanToggleOn(technology);
217             if (technology === 'wifi') {
218                 setTimeout(function() {
219                     /* add a 1 sec delay */
220                     connmanScan(id);
221                 }, 1000);
222             }
223         } else {
224             connmanToggleOff(technology);
225         }
226     }
227
228     if (property[0] === 'State') {
229         /* specific service has changed */
230         var service = $(jqId(id)).data('service-object');
231         if (service == null) {
232             console.error('Connman service not found ' + id);
233             return;
234         }
235
236         service.prop.State = property[1];
237         connmanUpdateService(service);
238     } else if (property[0] === 'Connected') {
239         /* unknown service has changed, sync Connman */
240         console.log('Unknown service connected property changed');
241         connmanSync(null, function(e) {
242             showMsg('Error', e);
243         });
244     }
245 }
246
247 function connmanClearAvailableList() {
248     $('#listview_services_available').html('');
249 }
250
251 function connmanScan(technology) {
252     if (technology !== '/net/connman/technology/wifi') {
253         /* Connman only supports wifi scan */
254         return;
255     }
256
257     if (connmanScanInProgress) {
258         console.log('Connman scan in progress...');
259         return;
260     }
261
262     console.log('Start connman scan');
263     connmanScanInProgress = true;
264
265     /* clear the services list with new scan */
266     connmanClearAvailableList();
267
268     showSpinner(false, 'Scanning...');
269     $('#toggle_connman_wifi').slider('disable');
270     $('#toggle_connman_wifi').slider('refresh');
271     settings.connman.scan(technology, function(services) {
272         hideSpinner();
273         connmanScanInProgress = false;
274         $('#toggle_connman_wifi').slider('enable');
275         $('#toggle_connman_wifi').slider('refresh');
276         console.log('found ' + services.length + ' connman services');
277         for (var i = 0; i < services.length; i++) {
278             var service = services[i];
279             if (service.prop.Type === undefined || service.prop.Type === 'bluetooth') {
280                 console.log('Ignore bluetooth or unknown services');
281                 continue;
282             }
283             if (service.prop.Name === undefined) {
284                 console.log('Ignore hidden service - ' + service.id);
285                 continue;
286             }
287
288             connmanUpdateService(service);
289         }
290     }, function(e) {
291         hideSpinner();
292         connmanScanInProgress = false;
293         $('#toggle_connman_wifi').slider('enable');
294         $('#toggle_connman_wifi').slider('refresh');
295         showMsg('Error', 'Cannot scan: ' + e);
296     });
297 }
298
299 function connmanSync(success_cb, error_cb) {
300     if (connmanScanInProgress) {
301         console.log('Connman scan in progress...');
302         return;
303     }
304
305     console.log('Start connman sync');
306     /* clear the services list with new scan */
307     connmanClearAvailableList();
308
309     wifiScanInProgress = true;
310     $('#toggle_connman_wifi').slider('disable');
311     $('#toggle_connman_wifi').slider('refresh');
312     settings.connman.getServices(function(services) {
313         connmanScanInProgress = false;
314         $('#toggle_connman_wifi').slider('enable');
315         $('#toggle_connman_wifi').slider('refresh');
316         console.log('found ' + services.length + ' connman services');
317         for (var i = 0; i < services.length; i++) {
318             var service = services[i];
319             if (service.prop.Type === undefined || service.prop.Type === 'bluetooth') {
320                 console.log('Ignore bluetooth or unknown services');
321                 continue;
322             }
323             if (service.prop.Name === undefined) {
324                 console.log('Ignore hidden service - ' + service.id);
325                 continue;
326             }
327
328             connmanUpdateService(service);
329         }
330         if (success_cb) {
331             success_cb();
332         }
333     }, function(e) {
334         connmanScanInProgress = false;
335         $('#toggle_connman_wifi').slider('enable');
336         $('#toggle_connman_wifi').slider('refresh');
337         if (error_cb) {
338             error_cb(e);
339         }
340     });
341 }
342
343 function connmanRefreshServicesList() {
344     $('#listview_services_available').listview('refresh');
345 }
346
347 function connmanAppendToAvailableList(service) {
348     if ($('#listview_services_available').find(jqId(service.id)).length != 0) return;
349
350     var parent = '#listview_services_available';
351     connmanConstructServiceElement(parent, service);
352     connmanUpdateServiceButton(service);
353     connmanRefreshServicesList();
354 }
355
356 function connmanRemoveFromAvailableList(service_id) {
357     var removeThis = $('#listview_services_available li').filter(function() {
358         return $(this).find(jqId(service_id)).length === 1;
359     });
360
361     if (removeThis.length !== 0) {
362         removeThis.remove();
363         connmanRefreshServicesList();
364     }
365 }
366
367 function getSignalStrengthStr(strength) {
368     var signal_strength = 'unknown';
369     if (strength > 0 && strength <= 20) {
370         strength = 'very poor';
371     } else if (strength > 20 && strength <= 40) {
372         signal_strength = 'poor';
373     } else if (strength > 40 && strength <= 70) {
374         signal_strength = 'average';
375     } else if (strength > 70 && strength <= 90) {
376         signal_strength = 'good';
377     } else if (strength > 90 && strength <= 100) {
378         signal_strength = 'excellent';
379     }
380     return signal_strength;
381 }
382
383 function connmanConstructTechnologyElement(technology) {
384     if (technology.prop.Type === undefined || technology.prop.Name == undefined) {
385         console.error('technology type or name missing ' + technology);
386         return;
387     }
388
389     var html = '<ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset">';
390     html += '<li data-role="fieldcontain">';
391     html += '<label for="toggle_connman_' + technology.prop.Type + '" class="ui-slider">' + technology.prop.Name + '</label>';
392     html += '<select data-role="slider" name="toggle_connman_' + technology.prop.Type + '" ';
393     html += 'id="toggle_connman_' + technology.prop.Type + '" class="ui-slider-switch">';
394     html += '<option value="off">Off</option>';
395     html += '<option value="on">On</option>';
396     html += '</select></li></ul>';
397     $('#connman_technologies').append(html).trigger('create');
398
399     console.log('Connman technology ' + technology.prop.Type + ' is powered: ' + technology.prop.Powered);
400     if (technology.prop.Powered) {
401         connmanToggleOn(technology.prop.Type);
402         if ($('ul#listview_services_available li').length === 0) {
403             /* connman only supports wifi scan */
404             if (technology.prop.Type === 'wifi') {
405                 connmanScan(technology.id);
406             } else {
407                 connmanSync(null, function(e) {
408                     showMsg('Error', e);
409                 });
410             }
411         }
412     } else {
413         connmanToggleOff(technology.prop.Type);
414     }
415
416     $('#toggle_connman_' + technology.prop.Type).change(function() {
417         console.log('toggle ' + technology.prop.Type + ' changed');
418
419         if (connmanScanInProgress) {
420             console.log('Connman scan in progress...');
421             return;
422         }
423
424         $('#toggle_connman_' + technology.prop.Type).slider('disable');
425         $('#toggle_connman_' + technology.prop.Type).slider('refresh');
426         if ($('#toggle_connman_' + technology.prop.Type).val() === 'off') {
427             technology.setPowered(false, function() {
428                 /* success */
429                 $('#toggle_connman_' + technology.prop.Type).slider('enable');
430                 $('#toggle_connman_' + technology.prop.Type).slider('refresh');
431                 console.log('Successfully disabled connman technology ' + technology.prop.Type);
432             }, function(e) {
433                 /* error */
434                 hideSpinner();
435                 $('#toggle_connman_' + technology.prop.Type).slider('enable');
436                 $('#toggle_connman_' + technology.prop.Type).val('on').slider('refresh');
437                 showMsg('Error', 'Cannot disable ' + technology.prop.Type);
438             });
439         } else {
440             technology.setPowered(true, function() {
441                 /* success */
442                 $('#toggle_connman_' + technology.prop.Type).slider('enable');
443                 $('#toggle_connman_' + technology.prop.Type).slider('refresh');
444                 if (technology.prop.Type === 'wifi') {
445                     setTimeout(function() {
446                         /* add a 1 sec delay */
447                         connmanScan(technology.id);
448                     }, 1000);
449                 }
450                 console.log('Successfully enabled connman technology ' + technology.prop.Type);
451             }, function(e) {
452                 /* error */
453                 hideSpinner();
454                 $('#toggle_connman_' + technology.prop.Type).slider('enable');
455                 $('#toggle_connman_' + technology.prop.Type).val('off').slider('refresh');
456                 showMsg('Error', 'Cannot enable ' + technology.prop.Type);
457             });
458         }
459     });
460 }
461
462 function connmanConstructServiceElement(parent, service) {
463     if (service.id === undefined || service.prop.Type === undefined) {
464         console.error('service id or type missing ' + service);
465         return;
466     }
467
468     var html = '<li data-icon="false"><a href="#" id="' + jqId(service.id).replace('#', '') + '">';
469     html += '<div class="service-ssid">' + service.prop.Name + '</div>';
470
471     if (service.prop.Type === 'wifi') {
472         html += '<div class="service-encryption">Encryption: ' + service.prop.EncryptionMode + '</div>';
473         html += '<div class="service-strength">Signal: ' + getSignalStrengthStr(service.prop.Strength) + '</div>';
474     }
475     html += '<div class="service-status"></div>';
476     html += '<div data-role="button" class="service-action-button ui-li-aside" data-inline="true"></div>';
477     html += '</a></li>';
478     $(parent).append(html).trigger('create');
479
480     /* store service object in the element so we can reference it later */
481     $(jqId(service.id)).data('service-object', service);
482
483     $(jqId(service.id)).on('click', function() {
484         /* BUG in webruntime that cause click event when another page is rendered */
485         if ($.mobile.activePage.attr('id') === 'page_connman') {
486             localStorage.setItem('connman_service_id', service.id);
487             console.log('Enter Connman service page');
488             $.mobile.changePage('#page_connman_service');
489         }
490     });
491
492     $(jqId(service.id)).find('div.service-action-button').on('click', function(e) {
493         var parent = $(this).parent().attr('id');
494
495         /*
496          * prevent the click event to propagate up
497          */
498         e.stopImmediatePropagation();
499         e.preventDefault();
500
501         /* retrieve the service object from element */
502         var service = $(jqId(parent)).data('service-object');
503         if (service == null) {
504             console.error('Connman service object not found');
505             return;
506         }
507
508         if (service.prop.State === 'idle') {
509             if (service.prop.EncryptionMode === 'none') {
510                 connmanConnectToService(service, null);
511             } else {
512                 connmanConnectToService(service, null, null, function(e) {
513                     console.log('Connect failed, will ask for passphrase');
514                     localStorage.setItem('connman_service_id', service.id);
515                     console.log('Enter connect WiFi page');
516                     $.mobile.changePage('#page_wifi_connect');
517                 });
518             }
519         } else if (service.prop.State === 'ready') {
520             console.log('Disconnecting from service: ' + service.prop.Name);
521             showSpinner(false, 'Disconnecting...');
522             service.disconnect(function() {
523                 /* success */
524                 connmanSync(function() {
525                     hideSpinner();
526                 }, function(e) {
527                     showMsg('Error', e);
528                 })
529             }, function(e) {
530                 /* error */
531                 hideSpinner();
532                 showMsg('Error', 'Disconnect service failed: ' + e);
533             });
534         }
535     });
536 }
537
538 function connmanTetheringPageReload() {
539     $('#connman_tethering_technologies').html('');
540     settings.connman.getTechnologies(function(technologies) {
541         for (var i = 0; i < technologies.length; i++) {
542             var technology = technologies[i];
543             console.log('Connman technology available for tethering: ' + technology.prop.Name);
544             connmanConstructTetheringElement(technology);
545         }
546     }, function(e) {
547         showMsg('Error', e);
548     });
549 }
550
551 function connmanConstructTetheringElement(technology) {
552     if (technology.prop.Type === undefined || technology.prop.Name == undefined) {
553         console.error('technology type or name missing ' + technology);
554         return;
555     }
556
557     var html = '<ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset">';
558     html += '<li data-role="fieldcontain">';
559     html += '<label for="toggle_tethering_' + technology.prop.Type + '" class="ui-slider">Tether ' + technology.prop.Name + '</label>';
560     html += '<select data-role="slider" name="toggle_tethering_' + technology.prop.Type + '" ';
561     html += 'id="toggle_tethering_' + technology.prop.Type + '" class="ui-slider-switch">';
562     html += '<option value="off">Off</option>';
563     html += '<option value="on">On</option>';
564     html += '</select></li></ul>';
565     $('#connman_tethering_technologies').append(html).trigger('create');
566
567     console.log('Connman technology ' + technology.prop.Type + ' is tethered: ' + technology.prop.Tethering);
568
569     if (technology.prop.Powered === true && technology.prop.Connected === true) {
570         if (technology.prop.Tethering === true) {
571             $('#toggle_tethering_' + technology.prop.Type).val('on').slider('refresh');
572             $('#input_wifi_tethering_id').textinput('disable');
573             $('#input_wifi_tethering_passphrase').textinput('disable');
574             /* WiFi-only options */
575             if (technology.prop.Type === 'wifi') {
576                 $('#connman_wifi_tethering_options').show();
577                 if (technology.prop.TetheringIdentifier) {
578                     $('#input_wifi_tethering_id').val(technology.prop.TetheringIdentifier);
579                 }
580                 if (technology.prop.TetheringPassphrase) {
581                     $('#input_wifi_tethering_passphrase').val(technology.prop.TetheringPassphrase);
582                 }
583             }
584         } else {
585             $('#toggle_tethering_' + technology.prop.Type).val('off').slider('refresh');
586         }
587     } else {
588         if (technology.prop.Type === 'wifi') {
589             $('#input_wifi_tethering_id').textinput('disable');
590             $('#input_wifi_tethering_passphrase').textinput('disable');
591         }
592         $('#toggle_tethering_' + technology.prop.Type).slider('disable');
593         $('#toggle_tethering_' + technology.prop.Type).slider('refresh');
594     }
595
596     $('#toggle_tethering_' + technology.prop.Type).change(function() {
597         if ($('#toggle_tethering_' + technology.prop.Type).val() === 'on') {
598             if (technology.prop.Type === 'wifi') {
599                 var tethering_id = $('#input_wifi_tethering_id').val();
600                 var tethering_passphrase = $('#input_wifi_tethering_passphrase').val();
601                 if (tethering_id === '' || tethering_passphrase === '') {
602                     connmanTetheringPageReload();
603                     showMsg('Error', 'Please provide identifier and passphrase');
604                     return;
605                 }
606
607                 technology.setTethering(true, tethering_id, tethering_passphrase, function() {
608                     /* success */
609                     console.log('Successfully tethered connman technology ' + technology.prop.Type);
610                     connmanTetheringPageReload();
611                 }, function(e) {
612                     /* error */
613                     connmanTetheringPageReload();
614                     showMsg('Error', 'Cannot tether ' + technology.prop.Type + ': ' + e);
615                 });
616             } else {
617                 technology.setTethering(true, '', '', function() {
618                     /* success */
619                     console.log('Successfully tethered connman technology ' + technology.prop.Type);
620                     connmanTetheringPageReload();
621                 }, function(e) {
622                     /* error */
623                     connmanTetheringPageReload();
624                     showMsg('Error', 'Cannot tether ' + technology.prop.Type + ': ' + e);
625                 });
626             }
627         } else {
628             technology.setTethering(false, '', '', function() {
629                 /* success */
630                 console.log('Successfully untethered connman technology ' + technology.prop.Type);
631                 connmanTetheringPageReload();
632             }, function(e) {
633                 /* error */
634                 connmanTetheringPageReload();
635                 showMsg('Error', 'Cannot untether ' + technology.prop.Type + ': ' + e);
636             });
637         }
638     });
639 }
640
641 function connmanUpdateService(service) {
642     connmanAppendToAvailableList(service);
643
644     /* update service button for allowed action */
645     connmanUpdateServiceButton(service);
646
647     /* update service connection status */
648     connmanUpdateConnectionStatus(service);
649
650     /* update service detail panel */
651     if ($.mobile.activePage.attr('id') === 'page_connman_service') {
652         var service_id = localStorage.getItem('connman_service_id');
653         if (service_id == undefined) return;
654         var service_object = $(jqId(service_id)).data('service-object');
655         if (service.id === service_object.id) {
656             connmanConstructServicePanel(service);
657         }
658     }
659 }
660
661 function connmanUpdateServiceButton(service) {
662     if (service.prop.State === 'ready') {
663         $(jqId(service.id)).find('div.service-action-button').find('span').text('Disconnect');
664     } else if (service.prop.State === 'idle' || service.prop.State === 'online') {
665         $(jqId(service.id)).find('div.service-action-button').find('span').text('Connect');
666     }
667 }
668
669 function connmanUpdateConnectionStatus(service) {
670     var status = 'disconnected';
671     if (service.prop.State === 'ready') {
672         $(jqId(service.id)).addClass('service-connected');
673         status = 'connected';
674     } else if (service.prop.State === 'idle' || service.prop.State === 'online') {
675         status = 'disconnected';
676         $(jqId(service.id)).removeClass('service-connected');
677     }
678
679     connmanUpdateConnectionStatusText(service, status);
680 }
681
682 function connmanUpdateConnectionStatusText(service, status) {
683     $(jqId(service.id)).find('div.service-status').text(status);
684 }
685
686 function connmanConstructServicePanel(service) {
687     var status_connected = 'No';
688
689     if (service == null) return;
690     if (service.prop.State === 'ready') status_connected = 'Yes';
691
692     $('#page_connman_service_content').html('');
693     var html = '<ul data-role="listview" id="listview_connman_service" data-inset="true" ' + 'class="service-list ui-listview">';
694     html += '<li id="connman_service_name"><h2>Name: ' + service.prop.Name + '</h2></li>';
695     html += '<li id="connman_service_type"><h2>Type: ' + service.prop.Type + '</h2></li>';
696     html += '<li id="connman_service_type"><h2>State: ' + service.prop.State + '</h2></li>';
697     if (service.prop.Type === 'ethernet' || service.prop.Type === 'wifi') {
698         if (service.prop.Type === 'wifi') {
699             html += '<li id="connman_service_bssid"><h2>SSID: ' + service.prop.BSSID + '</h2></li>';
700             html += '<li id="connman_service_encryption"><h2>Encryption: ' + service.prop.EncryptionMode + '</h2></li>';
701             html += '<li id="connman_service_strength"><h2>Signal Strength: ' + service.prop.Strength + '</h2></li>';
702         }
703         if (service.prop.State === 'ready') {
704             html += '<li id="connman_service_ip_address"><h2>IP Address: ' + service.prop.IPv4.Address + '</h2></li>';
705             html += '<li id="connman_servicel_gateway"><h2>Gateway: ' + service.prop.IPv4.Gateway + '</h2></li>';
706             html += '<li id="connman_service_netmask"><h2>Netmask: ' + service.prop.IPv4.Netmask + '</h2></li>';
707         }
708     }
709     html += '<li id="connman_service_connected"><h2>Connected: ' + status_connected + '</h2></li>';
710     html += '</ul>';
711     $('#page_connman_service_content').append(html).trigger('create');
712     $('#listview_connman_services').listview('refresh');
713
714     var html = '<ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset">';
715     html += '<li data-role="fieldcontain">';
716     html += '<label for="toggle_autoconnect" class="ui-slider">Auto Connect</label>';
717     html += '<select data-role="slider" name="toggle_autoconnect" ';
718     html += 'id="toggle_autoconnect" class="ui-slider-switch">';
719     html += '<option value="off">Off</option>';
720     html += '<option value="on">On</option>';
721     html += '</select></li></ul>';
722     $('#page_connman_service_content').append(html).trigger('create');
723
724     if (service.prop.AutoConnect) {
725         $('#toggle_autoconnect').val('on').slider('refresh');
726     } else {
727         $('#toggle_autoconnect').val('off').slider('refresh');
728     }
729
730     $('#toggle_autoconnect').change(function() {
731         if ($('#toggle_autoconnect').val() === 'on') {
732             service.setAutoConnect(true, function() {
733                 /* success */
734                 console.log('Successfully enabled autoconnect for ' + service.prop.Name);
735             }, function(e) {
736                 /* error */
737                 console.log('Cannot enable autoconnect: ' + e);
738             });
739         } else {
740             service.setAutoConnect(false, function() {
741                 /* success */
742                 console.log('Successfully disabled autoconnect for ' + service.prop.Name);
743             }, function(e) {
744                 /* error */
745                 console.log('Cannot disable autoconnect: ' + e);
746             });
747         }
748     });
749 }
750
751 function connmanUpdateServicePanel(service) {
752     var status_connected = 'No';
753
754     if (service == null) return;
755     if (service.prop.State === 'ready') status_connected = 'Yes';
756     $('#connman_service_connected').text(status_connected);
757     $('#listview_connman_service').listview('refresh');
758 }
759
760 function connmanToggleOn(technology_type) {
761     setTimeout(function() {
762         $('#toggle_connman_' + technology_type).val('on').slider('refresh');
763         console.log('Turn on toggle #toggle_connman_' + technology_type);
764     }, 1000);
765 }
766
767 function connmanToggleOff(technology_type) {
768     setTimeout(function() {
769         $('#toggle_connman_' + technology_type).val('off').slider('refresh');
770         console.log('Turn off toggle #toggle_connman_' + technology_type);
771     }, 1000);
772 }
773
774 function connmanConnectToService(service, passphrase, success_cb, error_cb) {
775     console.log('Connect to service: ' + service.prop.Name);
776     showSpinner(false, 'Connecting...');
777     service.connect(passphrase, function() {
778         /* success */
779         connmanSync(function() {
780             hideSpinner();
781             if (success_cb) {
782                 success_cb();
783             }
784         }, function(e) {
785             hideSpinner();
786             if (error_cb) {
787                 error_cb(e);
788             }
789         });
790     }, function(e) {
791         /* error */
792         hideSpinner();
793         if (error_cb) {
794             error_cb(e);
795         }
796     });
797 }
798
799 function connmanGetHiddenServicePath(security_type, success_cb, error_cb) {
800     if (security_type === undefined || success_cb === undefined || error_cb === undefined) return;
801     settings.connman.scan(function(services) {
802         connmanScanInProgress = false;
803         console.log('found ' + services.length + ' connman services');
804         for (var i = 0; i < services.length; i++) {
805             var service = services[i];
806             if (service.prop.Name === undefined && service.prop.Type === 'wifi') {
807                 console.log('Hidden network matched - ' + service.id);
808                 success_cb(service.id);
809                 return;
810             }
811         }
812         error_cb('No hidden network with security: ' + security_type);
813     }, function(e) {
814         connmanScanInProgress = false;
815         if (error_cb) {
816             error_cb(e);
817         }
818     });
819 }