2 * Copyright (c) 2013, Intel Corporation.
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
10 function wifiPanelInit() {
12 settings.wifi.subscribeEvents(wifiEventReceived);
14 /* WiFi Settings Panel */
15 $('#page_wifi').on('pageshow', function(event, data) {
16 if (data.prevPage.attr('id') === 'page_wifi_detail') return;
18 var adapter = settings.wifi.getDefaultAdapter();
19 if (adapter === null) {
20 showMsg('Error', 'WiFi adapter not found');
23 console.log('Default WiFi adapter: ', adapter.name);
25 if (adapter.name != undefined) {
26 $('#label_wifi_adapter').html(adapter.name);
28 $('#label_wifi_adapter').html('WiFi adapter not found');
31 adapter.getPowered(function(is_powered) {
32 if (adapter.powered) {
39 showMsg('Error', 'Cannot get WiFi state: ' + e);
43 $('#toggle_wifi').change(function() {
44 var wifi_switch = $(this);
45 var adapter = settings.wifi.getDefaultAdapter();
46 if (adapter === null) {
47 showMsg('Error', 'WiFi adapter not found');
52 if (wifi_switch[0].selectedIndex === 0) {
53 adapter.setPowered(false, function() {
55 console.log('Successfully disable wifi subsystem');
58 $('#toggle_wifi').val('on').slider('refresh');
59 showMsg('Error', 'Cannot disable WiFi subsystem: ' + e);
62 adapter.setPowered(true, function() {
64 console.log('Successfully enable WiFi subsystem');
68 $('#toggle_wifi').val('off').slider('refresh');
69 showMsg('Error', 'Cannot enable WiFi subsystem: ' + e);
74 $('#button_wifi_refresh').on('click', function() {
75 var adapter = settings.wifi.getDefaultAdapter();
76 if (adapter === null) {
77 showErr('Error', 'WiFi adapter not found');
83 $('#button_wifi_add').on('click', function() {
84 var adapter = settings.wifi.getDefaultAdapter();
85 if (adapter === null) {
86 showMsg('Error', 'WiFi adapter not found');
89 $.mobile.changePage('#page_wifi_add');
92 /* WiFi network detail page */
93 $('#page_wifi_detail').on('pageshow', function(event, data) {
94 console.log('Entering WiFi detail info page');
95 var network_id = localStorage.getItem('wifi_network_id');
96 if (network_id == undefined) return;
97 var network = $(jqId(network_id)).data('network-object');
98 wifiConstructDetailPanel(network);
101 /* WiFi add new network page */
102 $('#page_wifi_add').on('pagebeforeshow', function(event, data) {
103 console.log('Entering wifi add network page');
104 $('#input_wifi_ssid').val('');
105 $('#input_wifi_password').val('');
106 $('#select_wifi_security').val('None').change();
107 $('#input_wifi_password').textinput('disable');
110 $('#page_wifi_button_add').on('click', function() {
111 var adapter = settings.wifi.getDefaultAdapter();
112 var ssid = $('#input_wifi_ssid').val();
113 var security_option = $('#select_wifi_security').val();
114 var passcode = $('#input_wifi_password').val();
118 showMsg('Error', 'Enter SSID');
121 if (security_option !== 'None') {
122 showMsg('Error', 'Not supported');
126 showSpinner(false, 'Connecting...');
127 adapter.connectNetwork(ssid, security_option, passcode, function() {
130 $.mobile.changePage('#page_wifi');
135 showMsg('Error', 'WiFi connect failed: ' + e);
139 $('#select_wifi_security').on('change', function(event, data) {
140 var security = this.value;
141 console.log('selected WiFi security: ' + security);
143 if (security === 'None') {
144 $('#input_wifi_password').textinput('disable');
145 $('#input_wifi_password').val('');
147 $('#input_wifi_password').textinput('enable');
152 function wifiEventReceived(event) {
153 if (event.type !== WS_EVENT_TYPE.WIFI) return;
154 console.log('WiFi event received: [' + event.name + ', ' + event.value + ']');
155 if (event.name === 'enabled' && event.value === true) {
157 } else if (event.name === 'enabled' && event.value === false) {
162 function wifiClearKnownList() {
163 console.log('Clear known WiFi network list');
164 $('#listview_network_known').html('');
167 function wifiClearAvailableList() {
168 console.log('Clear available WiFi network list');
169 $('#listview_network_available').html('');
172 function wifiScan(adapter) {
173 console.log('Start wifi scan');
174 /* clear the network list with new scan */
175 wifiClearKnownList();
176 wifiClearAvailableList();
178 adapter.scan(function(networks) {
179 console.log('found ' + networks.length + ' networks');
180 for (var i = 0; i < networks.length; i++) {
181 var network = networks[i];
182 console.log('network ssid: ' + network.ssid);
183 console.log('network connected: ' + network.connected);
184 wifiUpdateNetwork(network);
187 showMsg('Error', 'Cannot scan: ' + e);
191 function wifiRefreshList() {
192 $('#listview_network_known').listview('refresh');
193 $('#listview_network_available').listview('refresh');
196 function wifiAppendToKnownList(network) {
197 console.log('Append to known network list - ' + network.ssid);
198 if ($('#listview_network_known').find(jqId(network.ssid)).length != 0) return;
200 var parent = '#listview_network_known';
201 wifiConstructNetworkElement(parent, network);
202 wifiUpdateNetworkButton(network);
206 function wifiRemoveFromKnownList(network_ssid) {
207 var removeThis = $('#listview_network_known li').filter(function() {
208 return $(this).find(jqId(network_ssid)).length === 1;
211 if (removeThis.length !== 0) {
212 console.log('Remove from known network list - ' + network_ssid);
218 function wifiAppendToAvailableList(network) {
219 console.log('Append to available list - ' + network.ssid);
220 if ($('#listview_network_available').find(jqId(network.ssid)).length != 0) return;
222 var parent = '#listview_network_available';
223 wifiConstructNetworkElement(parent, network);
224 wifiUpdateNetworkButton(network);
228 function wifiRemoveFromAvailableList(network_ssid) {
229 var removeThis = $('#listview_network_available li').filter(function() {
230 return $(this).find(jqId(network_ssid)).length === 1;
233 if (removeThis.length !== 0) {
234 console.log('Remove from available list: ' + network_ssid);
240 function wifiConstructNetworkElement(parent, network) {
241 var html = '<li data-icon="false"><a href="#" id="' + jqId(network.ssid).replace('#', '') + '">';
242 html += '<div class="network-ssid">' + network.ssid + '</div>';
243 html += '<div class="network-status"></div>';
244 html += '<div data-role="button" class="network-action-button ui-li-aside" data-inline="true"></div>';
246 $(parent).append(html).trigger('create');
248 /* store network object in the element so we can reference it later */
249 $(jqId(network.ssid)).data('network-object', network);
251 $(jqId(network.ssid)).on('click', function() {
252 localStorage.setItem('wifi_network_id', network.ssid);
253 $.mobile.changePage('#page_wifi_detail');
256 $(jqId(network.ssid)).find('div.network-action-button').on('click', function(e) {
257 var parent = $(this).parent().attr('id');
258 console.log('network action button clicked');
261 * prevent the click event to propagate up
263 e.stopImmediatePropagation();
266 var adapter = settings.wifi.getDefaultAdapter();
267 if (adapter === null) return;
269 /* retrieve the network object from element */
270 var network = $(jqId(parent)).data('network-object');
271 if (network == undefined) {
272 console.error('Wifi network not found');
276 if (!network.connected) {
277 createPopupDialog(false, false, 'Connect to network', network.ssid, 'Connect', 'Cancel', function() {
278 console.log('WiFi connect to network: ' + network.ssid);
279 showSpinner(false, 'Connecting...');
280 adapter.connectNetwork(network.ssid, null, null, function() {
283 network.connected = true;
285 /* changing from disconnected to connected */
286 wifiUpdateNetwork(network);
290 showMsg('Error', 'WiFi connect failed: ' + e);
294 console.log('Wifi disconnect from network: ' + network.ssid);
295 showSpinner(false, 'Disconnecting...');
296 adapter.disconnectNetwork(network.ssid, function() {
299 network.connected = false;
300 $('#button_wifi_disconnect').remove();
302 /* changing from connected to disconnected */
303 wifiUpdateNetwork(network);
307 showMsg('Error', 'Network disconnect failed: ' + e);
309 /* Something is wrong, remove from connected list */
310 wifiRemoveFromKnownList(network.ssid);
316 function wifiUpdateNetwork(network) {
317 console.log('Constructing or updating WiFi network');
319 /* reposition device if it's connected or disconnected */
320 if (network.connected) {
321 wifiRemoveFromAvailableList(network.ssid);
322 wifiAppendToKnownList(network);
324 wifiRemoveFromKnownList(network.ssid);
325 wifiAppendToAvailableList(network);
328 /* update network button for allowed action */
329 wifiUpdateNetworkButton(network);
331 /* update network connection status */
332 wifiUpdateConnectionStatus(network);
335 function wifiUpdateNetworkButton(network) {
336 if (network.connected) {
337 $(jqId(network.ssid)).find('div.network-action-button').find('span').text('Disconnect');
339 $(jqId(network.ssid)).find('div.network-action-button').find('span').text('Connect');
343 function wifiUpdateConnectionStatus(network) {
344 var status = 'disconnected';
345 if (network.connected) {
346 $(jqId(network.ssid)).addClass('network-connected');
347 status = 'connected';
349 status = 'disconnected';
350 $(jqId(network.ssid)).removeClass('network-connected');
353 wifiUpdateConnectionStatusText(network, status);
356 function wifiUpdateConnectionStatusText(network, status) {
357 $(jqId(network.ssid)).find('div.network-status').text(status);
360 function wifiConstructDetailPanel(network) {
361 var status_connected = 'No';
363 if (network == null) return;
364 if (network.connected) status_connected = 'Yes';
366 $('#page_wifi_detail_content').html('');
367 var html = '<ul data-role="listview" id="listview_wifi_detail" data-inset="true" ' + 'class="network-list ui-listview">';
368 html += '<li id="wifi_detail_ssid"><h2>SSID: ' + network.ssid + '</h2></li>';
369 html += '<li id="wifi_detail_connected"><h2>Connected: ' + status_connected + '</h2></li>';
371 $('#page_wifi_detail_content').append(html).trigger('create');
373 if (network.connected) {
374 html = '<div data-role="button" id="button_wifi_disconnect">Disconnect</div>';
375 $('#page_wifi_detail_content').append(html).trigger('create');
377 $('#button_wifi_disconnect').on('click', function(e) {
378 console.log('WiFi disconnect from network: ' + network.ssid);
379 /* retrieve the network object from element */
380 var adapter = settings.wifi.getDefaultAdapter();
381 if (adapter === null) return;
383 createPopupDialog(false, false, 'Disconnect from network', network.ssid, 'Disconnect', 'Cancel', function() {
384 showSpinner(false, 'Disconnecting...');
385 adapter.disconnectNetwork(network.ssid, function() {
388 network.connected = false;
389 $('#button_wifi_disconnect').remove();
391 /* changing from connected to disconnected */
392 wifiUpdateNetwork(network);
393 setTimeout(function() {
394 $.mobile.changePage('#page_wifi');
399 showMsg('Error', 'Network disconnect failed: ' + e);
401 /* Something is wrong, remove from connected list */
402 wifiRemoveFromKnownList(network.ssid);
407 $('#listview_wifi_detail').listview('refresh');
410 function wifiUpdateDetailPanel(network) {
411 var status_connected = 'No';
413 if (network == null) return;
414 if (network.connected) status_connected = 'Yes';
415 $('#wifi_detail_connected').text(status_connected);
416 $('#listview_wifi_detail').listview('refresh');
419 function wifiToggleOn() {
420 $('#wifi_networks').show();
421 $('#toggle_wifi').val('on').slider('refresh');
424 function wifiToggleOff() {
425 $('#wifi_networks').hide();
426 $('#toggle_wifi').val('off').slider('refresh');