Implements TIVI-2083, adds tethering support
[profile/ivi/SettingsApp.git] / js / panel-connman.js
index 76f840c..8d1ac8b 100644 (file)
@@ -63,6 +63,11 @@ function connmanPanelInit() {
         $.mobile.changePage('#page_wifi_add');
     });
 
+    /* Connman tethering page */
+    $('#page_connman_tethering').on('pageshow', function(event, data) {
+        connmanTetheringPageReload();
+    });
+
     /* Connman service detail page */
     $('#page_connman_service').on('pageshow', function(event, data) {
         var service_id = localStorage.getItem('connman_service_id');
@@ -530,6 +535,109 @@ function connmanConstructServiceElement(parent, service) {
     });
 }
 
+function connmanTetheringPageReload() {
+    $('#connman_tethering_technologies').html('');
+    settings.connman.getTechnologies(function(technologies) {
+        for (var i = 0; i < technologies.length; i++) {
+            var technology = technologies[i];
+            console.log('Connman technology available for tethering: ' + technology.prop.Name);
+            connmanConstructTetheringElement(technology);
+        }
+    }, function(e) {
+        showMsg('Error', e);
+    });
+}
+
+function connmanConstructTetheringElement(technology) {
+    if (technology.prop.Type === undefined || technology.prop.Name == undefined) {
+        console.error('technology type or name missing ' + technology);
+        return;
+    }
+
+    var html = '<ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset">';
+    html += '<li data-role="fieldcontain">';
+    html += '<label for="toggle_tethering_' + technology.prop.Type + '" class="ui-slider">Tether ' + technology.prop.Name + '</label>';
+    html += '<select data-role="slider" name="toggle_tethering_' + technology.prop.Type + '" ';
+    html += 'id="toggle_tethering_' + technology.prop.Type + '" class="ui-slider-switch">';
+    html += '<option value="off">Off</option>';
+    html += '<option value="on">On</option>';
+    html += '</select></li></ul>';
+    $('#connman_tethering_technologies').append(html).trigger('create');
+
+    console.log('Connman technology ' + technology.prop.Type + ' is tethered: ' + technology.prop.Tethering);
+
+    if (technology.prop.Powered === true && technology.prop.Connected === true) {
+        if (technology.prop.Tethering === true) {
+            $('#toggle_tethering_' + technology.prop.Type).val('on').slider('refresh');
+            $('#input_wifi_tethering_id').textinput('disable');
+            $('#input_wifi_tethering_passphrase').textinput('disable');
+            /* WiFi-only options */
+            if (technology.prop.Type === 'wifi') {
+                $('#connman_wifi_tethering_options').show();
+                if (technology.prop.TetheringIdentifier) {
+                    $('#input_wifi_tethering_id').val(technology.prop.TetheringIdentifier);
+                }
+                if (technology.prop.TetheringPassphrase) {
+                    $('#input_wifi_tethering_passphrase').val(technology.prop.TetheringPassphrase);
+                }
+            }
+        } else {
+            $('#toggle_tethering_' + technology.prop.Type).val('off').slider('refresh');
+        }
+    } else {
+        if (technology.prop.Type === 'wifi') {
+            $('#input_wifi_tethering_id').textinput('disable');
+            $('#input_wifi_tethering_passphrase').textinput('disable');
+        }
+        $('#toggle_tethering_' + technology.prop.Type).slider('disable');
+        $('#toggle_tethering_' + technology.prop.Type).slider('refresh');
+    }
+
+    $('#toggle_tethering_' + technology.prop.Type).change(function() {
+        if ($('#toggle_tethering_' + technology.prop.Type).val() === 'on') {
+            if (technology.prop.Type === 'wifi') {
+                var tethering_id = $('#input_wifi_tethering_id').val();
+                var tethering_passphrase = $('#input_wifi_tethering_passphrase').val();
+                if (tethering_id === '' || tethering_passphrase === '') {
+                    connmanTetheringPageReload();
+                    showMsg('Error', 'Please provide identifier and passphrase');
+                    return;
+                }
+
+                technology.setTethering(true, tethering_id, tethering_passphrase, function() {
+                    /* success */
+                    console.log('Successfully tethered connman technology ' + technology.prop.Type);
+                    connmanTetheringPageReload();
+                }, function(e) {
+                    /* error */
+                    connmanTetheringPageReload();
+                    showMsg('Error', 'Cannot tether ' + technology.prop.Type + ': ' + e);
+                });
+            } else {
+                technology.setTethering(true, '', '', function() {
+                    /* success */
+                    console.log('Successfully tethered connman technology ' + technology.prop.Type);
+                    connmanTetheringPageReload();
+                }, function(e) {
+                    /* error */
+                    connmanTetheringPageReload();
+                    showMsg('Error', 'Cannot tether ' + technology.prop.Type + ': ' + e);
+                });
+            }
+        } else {
+            technology.setTethering(false, '', '', function() {
+                /* success */
+                console.log('Successfully untethered connman technology ' + technology.prop.Type);
+                connmanTetheringPageReload();
+            }, function(e) {
+                /* error */
+                connmanTetheringPageReload();
+                showMsg('Error', 'Cannot untether ' + technology.prop.Type + ': ' + e);
+            });
+        }
+    });
+}
+
 function connmanUpdateService(service) {
     connmanAppendToAvailableList(service);