| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-bluetooth-page' is the settings page for managing bluetooth | 7 * 'settings-bluetooth-page' is the settings page for managing bluetooth |
| 8 * properties and devices. | 8 * properties and devices. |
| 9 * | 9 * |
| 10 * Example: | 10 * Example: |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 }, | 72 }, |
| 73 | 73 |
| 74 /** | 74 /** |
| 75 * Set to the name of the dialog to show. This page uses a single | 75 * Set to the name of the dialog to show. This page uses a single |
| 76 * paper-dialog to host one of two dialog elements, 'addDevice' or | 76 * paper-dialog to host one of two dialog elements, 'addDevice' or |
| 77 * 'pairDevice'. This allows a seamless transition between adding and | 77 * 'pairDevice'. This allows a seamless transition between adding and |
| 78 * pairing dialogs. Note: This property should be set before opening the | 78 * pairing dialogs. Note: This property should be set before opening the |
| 79 * dialog, and setting the property will not itself cause the dialog to | 79 * dialog, and setting the property will not itself cause the dialog to |
| 80 * open. | 80 * open. |
| 81 */ | 81 */ |
| 82 dialog: String, | 82 dialogId: String, |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * Current Pairing device. | 85 * Current Pairing device. |
| 86 * @type {?chrome.bluetooth.Device|undefined} | 86 * @type {?chrome.bluetooth.Device|undefined} |
| 87 */ | 87 */ |
| 88 pairingDevice: Object, | 88 pairingDevice: Object, |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Current Pairing event. | 91 * Current Pairing event. |
| 92 * @type {?chrome.bluetoothPrivate.PairingEvent|undefined} | 92 * @type {?chrome.bluetoothPrivate.PairingEvent|undefined} |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 this.bluetoothDeviceUpdatedListener_); | 161 this.bluetoothDeviceUpdatedListener_); |
| 162 this.bluetooth.onDeviceChanged.addListener( | 162 this.bluetooth.onDeviceChanged.addListener( |
| 163 this.bluetoothDeviceUpdatedListener_); | 163 this.bluetoothDeviceUpdatedListener_); |
| 164 | 164 |
| 165 this.bluetoothDeviceRemovedListener_ = | 165 this.bluetoothDeviceRemovedListener_ = |
| 166 this.onBluetoothDeviceRemoved_.bind(this); | 166 this.onBluetoothDeviceRemoved_.bind(this); |
| 167 this.bluetooth.onDeviceRemoved.addListener( | 167 this.bluetooth.onDeviceRemoved.addListener( |
| 168 this.bluetoothDeviceRemovedListener_); | 168 this.bluetoothDeviceRemovedListener_); |
| 169 | 169 |
| 170 // Request the inital adapter state. | 170 // Request the inital adapter state. |
| 171 this.bluetooth.getAdapterState( | 171 this.bluetooth.getAdapterState(this.bluetoothAdapterStateChangedListener_); |
| 172 this.bluetoothAdapterStateChangedListener_); | |
| 173 }, | 172 }, |
| 174 | 173 |
| 175 /** @override */ | 174 /** @override */ |
| 176 detached: function() { | 175 detached: function() { |
| 177 if (this.bluetoothAdapterStateChangedListener_) { | 176 if (this.bluetoothAdapterStateChangedListener_) { |
| 178 this.bluetooth.onAdapterStateChanged.removeListener( | 177 this.bluetooth.onAdapterStateChanged.removeListener( |
| 179 this.bluetoothAdapterStateChangedListener_); | 178 this.bluetoothAdapterStateChangedListener_); |
| 180 } | 179 } |
| 181 if (this.bluetoothDeviceUpdatedListener_) { | 180 if (this.bluetoothDeviceUpdatedListener_) { |
| 182 this.bluetooth.onDeviceAdded.removeListener( | 181 this.bluetooth.onDeviceAdded.removeListener( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 this.updateDeviceList_(); | 246 this.updateDeviceList_(); |
| 248 }, | 247 }, |
| 249 | 248 |
| 250 /** | 249 /** |
| 251 * Process bluetooth.onDeviceAdded and onDeviceChanged events. | 250 * Process bluetooth.onDeviceAdded and onDeviceChanged events. |
| 252 * @param {!chrome.bluetooth.Device} device | 251 * @param {!chrome.bluetooth.Device} device |
| 253 * @private | 252 * @private |
| 254 */ | 253 */ |
| 255 onBluetoothDeviceUpdated_: function(device) { | 254 onBluetoothDeviceUpdated_: function(device) { |
| 256 var address = device.address; | 255 var address = device.address; |
| 257 if (this.dialog && this.pairingDevice && | 256 if (this.dialogId && this.pairingDevice && |
| 258 this.pairingDevice.address == address) { | 257 this.pairingDevice.address == address) { |
| 259 this.pairingDevice = device; | 258 this.pairingDevice = device; |
| 260 } | 259 } |
| 261 var index = this.getDeviceIndex_(address); | 260 var index = this.getDeviceIndex_(address); |
| 262 if (index >= 0) { | 261 if (index >= 0) { |
| 263 // Use splice to update the item in order to update the dom-repeat lists. | 262 // Use splice to update the item in order to update the dom-repeat lists. |
| 264 // See https://github.com/Polymer/polymer/issues/3254. | 263 // See https://github.com/Polymer/polymer/issues/3254. |
| 265 this.splice('deviceList', index, 1, device); | 264 this.splice('deviceList', index, 1, device); |
| 266 return; | 265 return; |
| 267 } | 266 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 292 this.bluetoothPrivate.onPairing.addListener( | 291 this.bluetoothPrivate.onPairing.addListener( |
| 293 this.bluetoothPrivateOnPairingListener_); | 292 this.bluetoothPrivateOnPairingListener_); |
| 294 } | 293 } |
| 295 | 294 |
| 296 this.bluetooth.startDiscovery(function() { | 295 this.bluetooth.startDiscovery(function() { |
| 297 if (chrome.runtime.lastError) { | 296 if (chrome.runtime.lastError) { |
| 298 if (chrome.runtime.lastError.message == 'Failed to stop discovery') { | 297 if (chrome.runtime.lastError.message == 'Failed to stop discovery') { |
| 299 // May happen if also started elsewhere; ignore. | 298 // May happen if also started elsewhere; ignore. |
| 300 return; | 299 return; |
| 301 } | 300 } |
| 302 console.error('startDsicovery Error: ' + | 301 console.error( |
| 303 chrome.runtime.lastError.message); | 302 'startDsicovery Error: ' + chrome.runtime.lastError.message); |
| 304 } | 303 } |
| 305 }); | 304 }); |
| 306 }, | 305 }, |
| 307 | 306 |
| 308 /** @private */ | 307 /** @private */ |
| 309 stopDiscovery_: function() { | 308 stopDiscovery_: function() { |
| 310 if (!this.get('adapterState.discovering')) | 309 if (!this.get('adapterState.discovering')) |
| 311 return; | 310 return; |
| 312 | 311 |
| 313 if (this.bluetoothPrivateOnPairingListener_) { | 312 if (this.bluetoothPrivateOnPairingListener_) { |
| 314 this.bluetoothPrivate.onPairing.removeListener( | 313 this.bluetoothPrivate.onPairing.removeListener( |
| 315 this.bluetoothPrivateOnPairingListener_); | 314 this.bluetoothPrivateOnPairingListener_); |
| 316 this.bluetoothPrivateOnPairingListener_ = undefined; | 315 this.bluetoothPrivateOnPairingListener_ = undefined; |
| 317 } | 316 } |
| 318 | 317 |
| 319 this.bluetooth.stopDiscovery(function() { | 318 this.bluetooth.stopDiscovery(function() { |
| 320 if (chrome.runtime.lastError) { | 319 if (chrome.runtime.lastError) { |
| 321 console.error('Error stopping bluetooth discovery: ' + | 320 console.error( |
| 321 'Error stopping bluetooth discovery: ' + |
| 322 chrome.runtime.lastError.message); | 322 chrome.runtime.lastError.message); |
| 323 } | 323 } |
| 324 }); | 324 }); |
| 325 }, | 325 }, |
| 326 | 326 |
| 327 /** | 327 /** |
| 328 * Process bluetoothPrivate.onPairing events. | 328 * Process bluetoothPrivate.onPairing events. |
| 329 * @param {!chrome.bluetoothPrivate.PairingEvent} e | 329 * @param {!chrome.bluetoothPrivate.PairingEvent} e |
| 330 * @private | 330 * @private |
| 331 */ | 331 */ |
| 332 onBluetoothPrivateOnPairing_: function(e) { | 332 onBluetoothPrivateOnPairing_: function(e) { |
| 333 if (!this.dialog || !this.pairingDevice || | 333 if (!this.dialogId || !this.pairingDevice || |
| 334 e.device.address != this.pairingDevice.address) { | 334 e.device.address != this.pairingDevice.address) { |
| 335 return; | 335 return; |
| 336 } | 336 } |
| 337 if (e.pairing == chrome.bluetoothPrivate.PairingEventType.KEYS_ENTERED && | 337 if (e.pairing == chrome.bluetoothPrivate.PairingEventType.KEYS_ENTERED && |
| 338 e.passkey === undefined && this.pairingEvent) { | 338 e.passkey === undefined && this.pairingEvent) { |
| 339 // 'keysEntered' event might not include the updated passkey so preserve | 339 // 'keysEntered' event might not include the updated passkey so preserve |
| 340 // the current one. | 340 // the current one. |
| 341 e.passkey = this.pairingEvent.passkey; | 341 e.passkey = this.pairingEvent.passkey; |
| 342 } | 342 } |
| 343 this.pairingEvent = e; | 343 this.pairingEvent = e; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 return i; | 397 return i; |
| 398 } | 398 } |
| 399 return -1; | 399 return -1; |
| 400 }, | 400 }, |
| 401 | 401 |
| 402 /** | 402 /** |
| 403 * @param {!chrome.bluetooth.Device} device | 403 * @param {!chrome.bluetooth.Device} device |
| 404 * @return {string} The text to display for |device| in the device list. | 404 * @return {string} The text to display for |device| in the device list. |
| 405 * @private | 405 * @private |
| 406 */ | 406 */ |
| 407 getDeviceName_: function(device) { | 407 getDeviceName_: function(device) { return device.name || device.address; }, |
| 408 return device.name || device.address; | |
| 409 }, | |
| 410 | 408 |
| 411 /** | 409 /** |
| 412 * @param {!chrome.bluetooth.Device} device | 410 * @param {!chrome.bluetooth.Device} device |
| 413 * @return {boolean} | 411 * @return {boolean} |
| 414 * @private | 412 * @private |
| 415 */ | 413 */ |
| 416 deviceIsPairedOrConnecting_: function(device) { | 414 deviceIsPairedOrConnecting_: function(device) { |
| 417 return !!device.paired || !!device.connecting; | 415 return !!device.paired || !!device.connecting; |
| 418 }, | 416 }, |
| 419 | 417 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 | 465 |
| 468 /** | 466 /** |
| 469 * @param {!chrome.bluetooth.Device} device | 467 * @param {!chrome.bluetooth.Device} device |
| 470 * @private | 468 * @private |
| 471 */ | 469 */ |
| 472 forgetDevice_: function(device) { | 470 forgetDevice_: function(device) { |
| 473 this.bluetoothPrivate.forgetDevice(device.address, function() { | 471 this.bluetoothPrivate.forgetDevice(device.address, function() { |
| 474 if (chrome.runtime.lastError) { | 472 if (chrome.runtime.lastError) { |
| 475 console.error( | 473 console.error( |
| 476 'Error forgetting: ' + device.name + ': ' + | 474 'Error forgetting: ' + device.name + ': ' + |
| 477 chrome.runtime.lastError.message); | 475 chrome.runtime.lastError.message); |
| 478 } | 476 } |
| 479 this.updateDeviceList_(); | 477 this.updateDeviceList_(); |
| 480 }.bind(this)); | 478 }.bind(this)); |
| 481 }, | 479 }, |
| 482 | 480 |
| 483 /** | 481 /** |
| 484 * @param {string} dialog | 482 * @param {string} dialogId |
| 485 * @param {string} dialogToShow The name of the dialog. | 483 * @param {string} dialogToShow The name of the dialog. |
| 486 * @return {boolean} | 484 * @return {boolean} |
| 487 * @private | 485 * @private |
| 488 */ | 486 */ |
| 489 dialogIsVisible_(dialog, dialogToShow) { | 487 dialogIsVisible_(dialogId, dialogToShow) { return dialogToShow == dialogId; }, |
| 490 return dialogToShow == dialog; | |
| 491 }, | |
| 492 | 488 |
| 493 /** | 489 /** |
| 494 * @param {string} dialogId | 490 * @param {string} dialogId |
| 495 * @private | 491 * @private |
| 496 */ | 492 */ |
| 497 openDialog_: function(dialogId) { | 493 openDialog_: function(dialogId) { |
| 498 if (this.dialog) { | 494 if (this.dialogId) { |
| 499 // Dialog already opened, just update the contents. | 495 // Dialog already opened, just update the contents. |
| 500 this.dialog = dialogId; | 496 this.dialogId = dialogId; |
| 501 return; | 497 return; |
| 502 } | 498 } |
| 503 this.dialog = dialogId; | 499 this.dialogId = dialogId; |
| 504 // Call flush so that the dialog gets sized correctly before it is opened. | 500 // Call flush so that the dialog gets sized correctly before it is opened. |
| 505 Polymer.dom.flush(); | 501 Polymer.dom.flush(); |
| 506 var dialog = this.$$('#deviceDialog'); | 502 var dialog = this.$$('#deviceDialog'); |
| 507 dialog.open(); | 503 dialog.open(); |
| 508 dialog.focus(); | 504 dialog.focus(); |
| 509 }, | 505 }, |
| 510 | 506 |
| 511 /** @private */ | 507 /** @private */ |
| 512 closeDialog_: function() { | 508 closeDialog_: function() { |
| 513 if (!this.dialog) | 509 if (!this.dialogId) |
| 514 return; | 510 return; |
| 515 var dialog = this.$$('#deviceDialog'); | 511 var dialog = this.$$('#deviceDialog'); |
| 516 dialog.close(); | 512 dialog.close(); |
| 517 this.dialog = ''; | 513 this.dialogId = ''; |
| 518 this.pairingDevice = null; | 514 this.pairingDevice = null; |
| 519 this.pairingEvent = null; | 515 this.pairingEvent = null; |
| 520 }, | 516 }, |
| 521 | 517 |
| 522 /** @private */ | 518 /** @private */ |
| 523 onCloseDialog_: function(event) { this.closeDialog_(); }, | 519 onCloseDialog_: function(event) { this.closeDialog_(); }, |
| 524 | 520 |
| 525 /** @private */ | 521 /** @private */ |
| 526 onDialogOpened_: function() { this.startDiscovery_(); }, | 522 onDialogOpened_: function() { this.startDiscovery_(); }, |
| 527 | 523 |
| 528 /** @private */ | 524 /** @private */ |
| 529 onDialogClosed_: function() { this.stopDiscovery_(); }, | 525 onDialogClosed_: function() { this.stopDiscovery_(); }, |
| 530 }); | 526 }); |
| OLD | NEW |