| Index: chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.js
|
| diff --git a/chrome/browser/resources/settings/bluetooth_page/bluetooth_page.js b/chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.js
|
| similarity index 48%
|
| copy from chrome/browser/resources/settings/bluetooth_page/bluetooth_page.js
|
| copy to chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.js
|
| index 66db1a9cf1c5244464a27de0d3f4e7fc0440d63c..50ddb23928b645a73ec218eab8c71c5940345fea 100644
|
| --- a/chrome/browser/resources/settings/bluetooth_page/bluetooth_page.js
|
| +++ b/chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.js
|
| @@ -1,71 +1,37 @@
|
| -// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| /**
|
| * @fileoverview
|
| - * 'settings-bluetooth-page' is the settings page for managing bluetooth
|
| + * 'settings-bluetooth-subpage' is the settings subpage for managing bluetooth
|
| * properties and devices.
|
| - *
|
| - * Example:
|
| - * <core-animated-pages>
|
| - * <settings-bluetooth-page>
|
| - * </settings-bluetooth-page>
|
| - * ... other pages ...
|
| - * </core-animated-pages>
|
| */
|
|
|
| -var bluetoothPage = bluetoothPage || {
|
| - /**
|
| - * Set this to provide a fake implementation for testing.
|
| - * @type {Bluetooth}
|
| - */
|
| - bluetoothApiForTest: null,
|
| -
|
| - /**
|
| - * Set this to provide a fake implementation for testing.
|
| - * @type {BluetoothPrivate}
|
| - */
|
| - bluetoothPrivateApiForTest: null,
|
| -};
|
| -
|
| Polymer({
|
| - is: 'settings-bluetooth-page',
|
| + is: 'settings-bluetooth-subpage',
|
|
|
| behaviors: [I18nBehavior, CrScrollableBehavior],
|
|
|
| properties: {
|
| - /** Preferences state. */
|
| - prefs: {
|
| - type: Object,
|
| - notify: true,
|
| - },
|
| -
|
| - /** @private */
|
| - bluetoothEnabled_: {
|
| + /** Reflects the bluetooth-page property. */
|
| + bluetoothEnabled: {
|
| type: Boolean,
|
| - value: false,
|
| - observer: 'bluetoothEnabledChanged_',
|
| - },
|
| -
|
| - /** @private */
|
| - deviceListExpanded_: {
|
| - type: Boolean,
|
| - value: false,
|
| + notify: true,
|
| },
|
|
|
| /**
|
| - * The cached bluetooth adapter state.
|
| + * The bluetooth adapter state, cached by bluetooth-page.
|
| * @type {!chrome.bluetooth.AdapterState|undefined}
|
| - * @private
|
| */
|
| - adapterState_: Object,
|
| + adapterState: Object,
|
|
|
| - /**
|
| - * Whether or not a bluetooth device is connected.
|
| - * @private
|
| - */
|
| - deviceConnected_: Boolean,
|
| + /** Informs bluetooth-page whether to show the spinner in the header. */
|
| + showSpinner: {
|
| + type: Boolean,
|
| + notify: true,
|
| + computed: 'computeShowSpinner_(bluetoothEnabled, dialogId_)',
|
| + },
|
|
|
| /**
|
| * The ordered list of bluetooth devices.
|
| @@ -80,6 +46,17 @@ Polymer({
|
| },
|
|
|
| /**
|
| + * The ordered list of paired or connecting bluetooth devices.
|
| + * @type {!Array<!chrome.bluetooth.Device>}
|
| + */
|
| + pairedDeviceList_: {
|
| + type: Array,
|
| + value: /** @return {Array} */ function() {
|
| + return [];
|
| + },
|
| + },
|
| +
|
| + /**
|
| * Reflects the iron-list selecteditem property.
|
| * @type {!chrome.bluetooth.Device}
|
| * @private
|
| @@ -97,30 +74,26 @@ Polymer({
|
| * dialog and setting the property will not itself cause the dialog to open.
|
| * @private
|
| */
|
| - dialogId_: String,
|
| + dialogId_: {
|
| + type: String,
|
| + value: '',
|
| + },
|
|
|
| /**
|
| * Current Pairing device.
|
| - * @type {?chrome.bluetooth.Device|undefined}
|
| + * @type {!chrome.bluetooth.Device|undefined}
|
| * @private
|
| */
|
| pairingDevice_: Object,
|
|
|
| /**
|
| - * Current Pairing event.
|
| - * @type {?chrome.bluetoothPrivate.PairingEvent|undefined}
|
| - * @private
|
| - */
|
| - pairingEvent_: Object,
|
| -
|
| - /**
|
| * The translated error message to show when a connect error occurs.
|
| * @private
|
| */
|
| errorMessage_: String,
|
|
|
| /**
|
| - * Interface for bluetooth calls. May be overriden by tests.
|
| + * Interface for bluetooth calls. Set in bluetooth-page.
|
| * @type {Bluetooth}
|
| * @private
|
| */
|
| @@ -130,7 +103,7 @@ Polymer({
|
| },
|
|
|
| /**
|
| - * Interface for bluetoothPrivate calls. May be overriden by tests.
|
| + * Interface for bluetoothPrivate calls. Set in bluetooth-page.
|
| * @type {BluetoothPrivate}
|
| * @private
|
| */
|
| @@ -140,14 +113,10 @@ Polymer({
|
| },
|
| },
|
|
|
| - observers: ['deviceListChanged_(deviceList_.*)'],
|
| -
|
| - /**
|
| - * Listener for chrome.bluetooth.onAdapterStateChanged events.
|
| - * @type {function(!chrome.bluetooth.AdapterState)|undefined}
|
| - * @private
|
| - */
|
| - bluetoothAdapterStateChangedListener_: undefined,
|
| + observers: [
|
| + 'adapterStateChanged_(adapterState.*)',
|
| + 'deviceListChanged_(deviceList_.*)',
|
| + ],
|
|
|
| /**
|
| * Listener for chrome.bluetooth.onBluetoothDeviceAdded/Changed events.
|
| @@ -163,28 +132,8 @@ Polymer({
|
| */
|
| bluetoothDeviceRemovedListener_: undefined,
|
|
|
| - /**
|
| - * Listener for chrome.bluetoothPrivate.onPairing events.
|
| - * @type {function(!chrome.bluetoothPrivate.PairingEvent)|undefined}
|
| - * @private
|
| - */
|
| - bluetoothPrivateOnPairingListener_: undefined,
|
| -
|
| - /** @override */
|
| - ready: function() {
|
| - if (bluetoothPage.bluetoothApiForTest)
|
| - this.bluetooth = bluetoothPage.bluetoothApiForTest;
|
| - if (bluetoothPage.bluetoothPrivateApiForTest)
|
| - this.bluetoothPrivate = bluetoothPage.bluetoothPrivateApiForTest;
|
| - },
|
| -
|
| /** @override */
|
| attached: function() {
|
| - this.bluetoothAdapterStateChangedListener_ =
|
| - this.onBluetoothAdapterStateChanged_.bind(this);
|
| - this.bluetooth.onAdapterStateChanged.addListener(
|
| - this.bluetoothAdapterStateChangedListener_);
|
| -
|
| this.bluetoothDeviceUpdatedListener_ =
|
| this.onBluetoothDeviceUpdated_.bind(this);
|
| this.bluetooth.onDeviceAdded.addListener(
|
| @@ -196,9 +145,6 @@ Polymer({
|
| this.onBluetoothDeviceRemoved_.bind(this);
|
| this.bluetooth.onDeviceRemoved.addListener(
|
| this.bluetoothDeviceRemovedListener_);
|
| -
|
| - // Request the inital adapter state.
|
| - this.bluetooth.getAdapterState(this.bluetoothAdapterStateChangedListener_);
|
| },
|
|
|
| /** @override */
|
| @@ -213,28 +159,25 @@ Polymer({
|
| this.bluetooth.onDeviceChanged.removeListener(
|
| this.bluetoothDeviceUpdatedListener_);
|
| }
|
| - if (this.bluetoothDeviceRemovedListener_) {
|
| - this.bluetooth.onDeviceRemoved.removeListener(
|
| - this.bluetoothDeviceRemovedListener_);
|
| - }
|
| },
|
|
|
| /** @private */
|
| - bluetoothEnabledChanged_: function() {
|
| - // When bluetooth is enabled, auto-expand the device list.
|
| - if (this.bluetoothEnabled_)
|
| - this.deviceListExpanded_ = true;
|
| + computeShowSpinner_: function() {
|
| + return this.bluetoothEnabled && !this.dialogId_;
|
| + },
|
| +
|
| + /** @private */
|
| + adapterStateChanged_: function() {
|
| + this.updateDeviceList_();
|
| },
|
|
|
| /** @private */
|
| deviceListChanged_: function() {
|
| - for (var i = 0; i < this.deviceList_.length; ++i) {
|
| - if (this.deviceList_[i].connected) {
|
| - this.deviceConnected_ = true;
|
| - return;
|
| - }
|
| - }
|
| - this.deviceConnected_ = false;
|
| + var devices = this.$.devices;
|
| + this.pairedDeviceList_ = this.deviceList_.filter(function(device) {
|
| + return !!device.paired || !!device.connecting;
|
| + });
|
| + this.updateScrollableContents();
|
| },
|
|
|
| /** @private */
|
| @@ -244,82 +187,21 @@ Polymer({
|
| },
|
|
|
| /**
|
| - * @return {string}
|
| - * @private
|
| - */
|
| - getIcon_: function() {
|
| - if (!this.bluetoothEnabled_)
|
| - return 'settings:bluetooth-disabled';
|
| - if (this.deviceConnected_)
|
| - return 'settings:bluetooth-connected';
|
| - return 'settings:bluetooth';
|
| - },
|
| -
|
| - /**
|
| - * @return {string}
|
| - * @private
|
| - */
|
| - getTitle_: function() {
|
| - return this.i18n(
|
| - this.bluetoothEnabled_ ? 'bluetoothEnabled' : 'bluetoothDisabled');
|
| - },
|
| -
|
| - /** @private */
|
| - toggleDeviceListExpanded_: function() {
|
| - this.deviceListExpanded_ = !this.deviceListExpanded_;
|
| - },
|
| -
|
| - /**
|
| - * @return {boolean} Whether the <iron-collapse> can be shown.
|
| - * @private
|
| - */
|
| - canShowDeviceList_: function() {
|
| - return this.bluetoothEnabled_ && this.deviceListExpanded_;
|
| - },
|
| -
|
| - /**
|
| * If bluetooth is enabled, request the complete list of devices and update
|
| * this.deviceList_.
|
| * @private
|
| */
|
| updateDeviceList_: function() {
|
| - if (!this.bluetoothEnabled_) {
|
| + if (!this.bluetoothEnabled) {
|
| this.deviceList_ = [];
|
| return;
|
| }
|
| this.bluetooth.getDevices(function(devices) {
|
| this.deviceList_ = devices;
|
| - this.updateScrollableContents();
|
| }.bind(this));
|
| },
|
|
|
| /**
|
| - * Event called when a user action changes the bluetoothEnabled state.
|
| - * @private
|
| - */
|
| - onBluetoothEnabledChange_: function() {
|
| - this.bluetoothPrivate.setAdapterState(
|
| - {powered: this.bluetoothEnabled_}, function() {
|
| - if (chrome.runtime.lastError) {
|
| - console.error(
|
| - 'Error enabling bluetooth: ' +
|
| - chrome.runtime.lastError.message);
|
| - }
|
| - });
|
| - },
|
| -
|
| - /**
|
| - * Process bluetooth.onAdapterStateChanged events.
|
| - * @param {!chrome.bluetooth.AdapterState} state
|
| - * @private
|
| - */
|
| - onBluetoothAdapterStateChanged_: function(state) {
|
| - this.adapterState_ = state;
|
| - this.bluetoothEnabled_ = state.powered;
|
| - this.updateDeviceList_();
|
| - },
|
| -
|
| - /**
|
| * Process bluetooth.onDeviceAdded and onDeviceChanged events.
|
| * @param {!chrome.bluetooth.Device} device
|
| * @private
|
| @@ -330,7 +212,9 @@ Polymer({
|
| this.pairingDevice_.address == address) {
|
| this.pairingDevice_ = device;
|
| }
|
| - var index = this.getDeviceIndex_(address);
|
| + var index = this.deviceList_.findIndex(function(device) {
|
| + return device.address == address;
|
| + });
|
| if (index >= 0) {
|
| this.set('deviceList_.' + index, device);
|
| return;
|
| @@ -345,76 +229,44 @@ Polymer({
|
| */
|
| onBluetoothDeviceRemoved_: function(device) {
|
| var address = device.address;
|
| - var index = this.getDeviceIndex_(address);
|
| - if (index < 0)
|
| - return;
|
| - this.splice('deviceList_', index, 1);
|
| + var index = this.deviceList_.findIndex(function(device) {
|
| + return device.address == address;
|
| + });
|
| + if (index >= 0)
|
| + this.splice('deviceList_', index, 1);
|
| },
|
|
|
| /** @private */
|
| startDiscovery_: function() {
|
| - if (!this.adapterState_ || this.adapterState_.discovering)
|
| + if (!this.adapterState || this.adapterState.discovering)
|
| return;
|
|
|
| - if (!this.bluetoothPrivateOnPairingListener_) {
|
| - this.bluetoothPrivateOnPairingListener_ =
|
| - this.onBluetoothPrivateOnPairing_.bind(this);
|
| - this.bluetoothPrivate.onPairing.addListener(
|
| - this.bluetoothPrivateOnPairingListener_);
|
| - }
|
| -
|
| this.bluetooth.startDiscovery(function() {
|
| - if (chrome.runtime.lastError) {
|
| - if (chrome.runtime.lastError.message == 'Failed to stop discovery') {
|
| - // May happen if also started elsewhere; ignore.
|
| - return;
|
| - }
|
| - console.error(
|
| - 'startDiscovery Error: ' + chrome.runtime.lastError.message);
|
| + var lastError = chrome.runtime.lastError;
|
| + if (lastError) {
|
| + if (lastError.message == 'Starting discovery failed')
|
| + return; // May happen if also started elsewhere, ignore.
|
| + console.error('startDiscovery Error: ' + lastError.message);
|
| }
|
| });
|
| },
|
|
|
| /** @private */
|
| stopDiscovery_: function() {
|
| - if (!this.get('adapterState_.discovering'))
|
| + if (!this.get('adapterState.discovering'))
|
| return;
|
|
|
| - if (this.bluetoothPrivateOnPairingListener_) {
|
| - this.bluetoothPrivate.onPairing.removeListener(
|
| - this.bluetoothPrivateOnPairingListener_);
|
| - this.bluetoothPrivateOnPairingListener_ = undefined;
|
| - }
|
| -
|
| this.bluetooth.stopDiscovery(function() {
|
| - if (chrome.runtime.lastError) {
|
| - console.error(
|
| - 'Error stopping bluetooth discovery: ' +
|
| - chrome.runtime.lastError.message);
|
| + var lastError = chrome.runtime.lastError;
|
| + if (lastError) {
|
| + if (lastError.message == 'Failed to stop discovery')
|
| + return; // May happen if also stopped elsewhere, ignore.
|
| + console.error('stopDiscovery Error: ' + lastError.message);
|
| }
|
| });
|
| },
|
|
|
| /**
|
| - * Process bluetoothPrivate.onPairing events.
|
| - * @param {!chrome.bluetoothPrivate.PairingEvent} e
|
| - * @private
|
| - */
|
| - onBluetoothPrivateOnPairing_: function(e) {
|
| - if (!this.dialogId_ || !this.pairingDevice_ ||
|
| - e.device.address != this.pairingDevice_.address) {
|
| - return;
|
| - }
|
| - if (e.pairing == chrome.bluetoothPrivate.PairingEventType.KEYS_ENTERED &&
|
| - e.passkey === undefined && this.pairingEvent_) {
|
| - // 'keysEntered' event might not include the updated passkey so preserve
|
| - // the current one.
|
| - e.passkey = this.pairingEvent_.passkey;
|
| - }
|
| - this.pairingEvent_ = e;
|
| - },
|
| -
|
| - /**
|
| * @param {!Event} e
|
| * @private
|
| */
|
| @@ -441,68 +293,27 @@ Polymer({
|
| },
|
|
|
| /**
|
| - * Handle a response sent from the pairing dialog and pass it to the
|
| - * bluetoothPrivate API.
|
| - * @param {Event} e
|
| - * @private
|
| - */
|
| - onResponse_: function(e) {
|
| - var options =
|
| - /** @type {!chrome.bluetoothPrivate.SetPairingResponseOptions} */ (
|
| - e.detail);
|
| - this.bluetoothPrivate.setPairingResponse(options, function() {
|
| - if (chrome.runtime.lastError) {
|
| - // TODO(stevenjb): Show error.
|
| - console.error(
|
| - 'Error setting pairing response: ' + options.device.name +
|
| - ': Response: ' + options.response +
|
| - ': Error: ' + chrome.runtime.lastError.message);
|
| - }
|
| - this.$$('#deviceDialog').close();
|
| - }.bind(this));
|
| - },
|
| -
|
| - /**
|
| - * @param {string} address
|
| - * @return {number} The index of the device associated with |address| or -1.
|
| - * @private
|
| - */
|
| - getDeviceIndex_: function(address) {
|
| - var len = this.deviceList_.length;
|
| - for (var i = 0; i < len; ++i) {
|
| - if (this.deviceList_[i].address == address)
|
| - return i;
|
| - }
|
| - return -1;
|
| - },
|
| -
|
| - /**
|
| - * @param {!chrome.bluetooth.Device} device
|
| - * @return {string} The text to display for |device| in the device list.
|
| + * @return {string}
|
| * @private
|
| */
|
| - getDeviceName_: function(device) {
|
| - return device.name || device.address;
|
| + getOffOnString_: function() {
|
| + return this.i18n(this.bluetoothEnabled ? 'bluetoothOn' : 'bluetoothOff');
|
| },
|
|
|
| /**
|
| - * @return {!Array<!chrome.bluetooth.Device>}
|
| + * @return {boolean}
|
| * @private
|
| */
|
| - getPairedOrConnecting_: function() {
|
| - return this.deviceList_.filter(function(device) {
|
| - return !!device.paired || !!device.connecting;
|
| - });
|
| + showDevices_: function() {
|
| + return this.bluetoothEnabled && this.pairedDeviceList_.length > 0;
|
| },
|
|
|
| /**
|
| - * @return {boolean} True if deviceList contains any paired devices.
|
| + * @return {boolean}
|
| * @private
|
| */
|
| - haveDevices_: function() {
|
| - return this.deviceList_.findIndex(function(d) {
|
| - return !!d.paired;
|
| - }) != -1;
|
| + showNoDevices_: function() {
|
| + return this.bluetoothEnabled && this.pairedDeviceList_.length == 0;
|
| },
|
|
|
| /**
|
| @@ -510,12 +321,9 @@ Polymer({
|
| * @private
|
| */
|
| connectDevice_: function(device) {
|
| - // If the device is not paired, show the pairing dialog.
|
| + // If the device is not paired, show the pairing dialog before connecting.
|
| if (!device.paired) {
|
| - // Set the pairing device and clear any pairing event.
|
| this.pairingDevice_ = device;
|
| - this.pairingEvent_ = null;
|
| -
|
| this.openDialog_('pairDevice');
|
| }
|
|
|
| @@ -536,11 +344,11 @@ Polymer({
|
| }
|
|
|
| if (!error) {
|
| - this.$$('#deviceDialog').close();
|
| + this.$.deviceDialog.close();
|
| return;
|
| }
|
|
|
| - var name = this.getDeviceName_(device);
|
| + var name = device.name || device.address;
|
| var id = 'bluetooth_connect_' + error;
|
| if (this.i18nExists(id)) {
|
| this.errorMessage_ = this.i18n(id, name);
|
| @@ -583,16 +391,6 @@ Polymer({
|
|
|
| /**
|
| * @param {string} dialogId
|
| - * @param {string} dialogToShow The name of the dialog.
|
| - * @return {boolean}
|
| - * @private
|
| - */
|
| - dialogIsVisible_: function(dialogId, dialogToShow) {
|
| - return dialogToShow == dialogId;
|
| - },
|
| -
|
| - /**
|
| - * @param {string} dialogId
|
| * @private
|
| */
|
| openDialog_: function(dialogId) {
|
| @@ -604,8 +402,7 @@ Polymer({
|
| this.dialogId_ = dialogId;
|
| // Call flush so that the dialog gets sized correctly before it is opened.
|
| Polymer.dom.flush();
|
| - var dialog = this.$$('#deviceDialog');
|
| - dialog.open();
|
| + this.$.deviceDialog.open();
|
| this.startDiscovery_();
|
| },
|
|
|
| @@ -613,15 +410,6 @@ Polymer({
|
| onDialogClosed_: function() {
|
| this.stopDiscovery_();
|
| this.dialogId_ = '';
|
| - this.pairingDevice_ = null;
|
| - this.pairingEvent_ = null;
|
| - },
|
| -
|
| - /**
|
| - * @param {Event} e
|
| - * @private
|
| - */
|
| - stopTap_: function(e) {
|
| - e.stopPropagation();
|
| + this.pairingDevice_ = undefined;
|
| },
|
| });
|
|
|