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 * A bluetooth device. | 6 * A bluetooth device. |
7 * @constructor | 7 * @constructor |
8 */ | 8 */ |
9 var BluetoothDevice = function() { | 9 var BluetoothDevice = function() { |
10 this.address = ''; | 10 this.address = ''; |
11 this.alias = ''; | 11 this.alias = ''; |
12 | |
13 // The text label of the selected device class. | |
12 this.class = 'Computer'; | 14 this.class = 'Computer'; |
15 | |
16 // The uint32 value of the selected device class. | |
13 this.classValue = 0; | 17 this.classValue = 0; |
14 this.isPairable = true; | 18 |
15 this.isTrusted = true; | 19 this.isTrusted = true; |
16 this.name = ''; | 20 this.name = ''; |
17 this.path = ''; | 21 this.path = ''; |
18 this.pairMethod = 'None'; | 22 |
23 // The label of the selected pairing method option. | |
24 this.pairingMethod = 'None'; | |
25 | |
26 // The text containing a PIN key or passkey for pairing. | |
27 this.pairingAuthToken = ''; | |
19 }; | 28 }; |
20 | 29 |
21 Polymer({ | 30 Polymer({ |
22 is: 'bluetooth-settings', | 31 is: 'bluetooth-settings', |
23 | 32 |
24 properties: { | 33 properties: { |
25 /** | 34 /** |
26 * The title to be displayed in a heading element for the element. | 35 * The title to be displayed in a heading element for the element. |
27 */ | 36 */ |
28 title: { | 37 title: { |
29 type: String | 38 type: String |
30 }, | 39 }, |
31 | 40 |
32 /** | 41 /** |
33 * Indicates whether or not the main bluetooth adapter is turned on. | |
34 */ | |
35 powerToMainAdapter: { | |
36 type: Boolean | |
37 }, | |
38 | |
39 /** | |
40 * A set of bluetooth devices. | 42 * A set of bluetooth devices. |
41 * @type !Array<!BluetoothDevice> | 43 * @type !Array<!BluetoothDevice> |
42 */ | 44 */ |
43 devices: { | 45 devices: { |
44 type: Array, | 46 type: Array, |
45 value: function() { return []; } | 47 value: function() { return []; } |
46 }, | 48 }, |
47 | 49 |
48 /** | 50 /** |
49 * A set of options for the possible bluetooth device classes/types. | 51 * A set of options for the possible bluetooth device classes/types. |
50 * Object |value| attribute comes from values in the WebUI, set in | 52 * Object |value| attribute comes from values in the WebUI, set in |
51 * setDeviceClassOptions. | 53 * setDeviceClassOptions. |
52 * @type !Array<! { text: string, value: int }} > | 54 * @type !Array<! { text: string, value: int }} > |
stevenjb
2015/07/30 18:28:51
nit: No ' ' after { or before }
rfrappier
2015/07/30 22:21:38
Done.
| |
53 */ | 55 */ |
54 deviceClassOptions: { | 56 deviceClassOptions: { |
55 type: Array, | 57 type: Array, |
56 value: function() { | 58 value: function() { |
57 return [{ text: 'Unknown', value: 0 }, | 59 return [{ text: 'Unknown', value: 0 }, |
58 { text: 'Mouse', value: 0x2580 }, | 60 { text: 'Mouse', value: 0x2580 }, |
59 { text: 'Keyboard', value: 0x2540 }, | 61 { text: 'Keyboard', value: 0x2540 }, |
60 { text: 'Audio', value: 0x240408 }, | 62 { text: 'Audio', value: 0x240408 }, |
61 { text: 'Phone', value: 0x7a020c }, | 63 { text: 'Phone', value: 0x7a020c }, |
62 { text: 'Computer', value: 0x100 }]; | 64 { text: 'Computer', value: 0x104 }]; |
63 } | 65 } |
64 }, | 66 }, |
65 | 67 |
66 /** | 68 /** |
67 * A set of strings representing the method to be used for | 69 * A set of strings representing the method to be used for |
68 * authenticating a device during a pair request. | 70 * authenticating a device during a pair request. |
69 * @type !Array<!String> | 71 * @type !Array<!String> |
70 */ | 72 */ |
71 deviceAuthenticationMethods: { | 73 deviceAuthenticationMethods: { |
72 type: Array, | 74 type: Array, |
73 value: function() { return ['None', 'Pin Code', 'Pass Key']; } | 75 value: function() { return ['None', 'PIN Code', 'PassKey']; } |
76 }, | |
77 | |
78 /** | |
79 * Contains keys for all the device paths which have been discovered. | |
stevenjb
2015/07/30 18:28:51
@type
rfrappier
2015/07/30 22:21:38
Done.
| |
80 */ | |
81 devicePaths: { | |
82 type: Object, | |
83 value: function() { return []; } | |
xiyuan
2015/07/30 17:21:22
Should we return "{}" for an Object?
stevenjb
2015/07/30 18:28:51
It actually looks like this should be type: Array
xiyuan
2015/07/30 18:33:57
|devicePaths| is used as a dict to look up whether
| |
74 }, | 84 }, |
75 }, | 85 }, |
76 | 86 |
77 ready: function() { | 87 ready: function() { |
78 this.title = 'Bluetooth Settings'; | 88 this.title = 'Bluetooth Settings'; |
79 }, | 89 }, |
80 | 90 |
91 // Checks whether or not the PIN/passkey input field should be shown. | |
92 // It should only be shown when the pair method is not 'None' or empty. | |
stevenjb
2015/07/30 18:28:51
@oaram {string} pairMethod
@return {boolean} Wheth
rfrappier
2015/07/30 22:21:38
Done.
| |
93 showAuthToken: function(pairMethod) { | |
94 return pairMethod != 'None' && pairMethod != undefined; | |
95 }, | |
96 | |
97 // Called by the WebUI which provides a list of devices which are connected | |
98 // to the main adapter. | |
stevenjb
2015/07/30 18:28:51
@param ...
rfrappier
2015/07/30 22:21:38
Done.
| |
99 updateBluetoothInfo: function(devices) { | |
100 device_list = []; | |
xiyuan
2015/07/30 17:21:21
var deviceList = [];
stevenjb
2015/07/30 18:28:51
Or actually:
/** @type {!Array<!BluetoothDevice>}
xiyuan
2015/07/30 18:33:57
device_list -< deviceList, jsdoc is nice.
rfrappier
2015/07/30 22:21:38
Done.
xiyuan
2015/07/30 23:30:37
It would be nice to follow stevenjb@'s suggestion
rfrappier
2015/07/31 00:55:59
Done.
| |
101 | |
102 for (var i = 0; i < devices.length; ++i) { | |
103 if (this.devicePaths[devices[i]['path']] == undefined) { | |
stevenjb
2015/07/30 18:28:51
if (this.devicePaths[devices[i]['path']] != undefi
rfrappier
2015/07/30 22:21:38
Done.
| |
104 // Get the label for the device class which should be selected. | |
105 var selectedClass = | |
106 this.getTextForDeviceClass(devices[i]['classValue']); | |
107 devices[i].class = selectedClass; | |
108 device_list.push(devices[i]); | |
109 this.devicePaths[devices[i]['path']] = true; | |
110 } | |
111 } | |
112 | |
113 this.devices = device_list; | |
xiyuan
2015/07/30 17:21:21
|device_list| contains only new devices at this po
rfrappier
2015/07/30 22:21:38
This function is only called in the very beginning
xiyuan
2015/07/30 23:30:37
If we are sure this is only intended to be called
rfrappier
2015/07/31 00:55:59
Done.
| |
114 }, | |
115 | |
81 pairDevice: function(e) { | 116 pairDevice: function(e) { |
82 var device = this.devices[e.path[2].dataIndex]; | 117 var device = this.devices[e.path[2].dataIndex]; |
83 device.classValue = this.getValueForDeviceClass(device.class); | 118 device.classValue = this.getValueForDeviceClass(device.class); |
119 this.devicePaths[device.path] = true; | |
84 | 120 |
85 // Send device info to the WebUI. | 121 // Send device info to the WebUI. |
86 chrome.send('requestBluetoothPair', [device]); | 122 chrome.send('requestBluetoothPair', [device]); |
87 }, | 123 }, |
88 | 124 |
89 discoverDevice: function(e) { | 125 discoverDevice: function(e) { |
90 var device = this.devices[e.path[2].dataIndex]; | 126 var device = this.devices[e.path[2].dataIndex]; |
91 device.classValue = this.getValueForDeviceClass(device.class); | 127 device.classValue = this.getValueForDeviceClass(device.class); |
128 this.devicePaths[device.path] = true; | |
92 | 129 |
93 // Send device info to WebUI. | 130 // Send device info to WebUI. |
94 chrome.send('requestBluetoothDiscover', [device]); | 131 chrome.send('requestBluetoothDiscover', [device]); |
95 }, | 132 }, |
96 | 133 |
134 // Adds a new device with default settings to the list of devices. | |
97 appendNewDevice: function() { | 135 appendNewDevice: function() { |
98 this.push('devices', new BluetoothDevice()); | 136 this.push('devices', new BluetoothDevice()); |
99 }, | 137 }, |
100 | 138 |
101 getValueForDeviceClass: function(classValue) { | 139 // This is called when a new device is discovered by the main adapter. |
140 // The device is only added to the view's list if it is not already in | |
141 // the list (i.e. its path has not yet been recorded in |devicePaths|). | |
stevenjb
2015/07/30 18:28:51
@param
rfrappier
2015/07/30 22:21:38
Done.
| |
142 addBluetoothDevice: function(device) { | |
143 if (this.devicePaths[device['path']] == undefined) { | |
stevenjb
2015/07/30 18:28:51
if (this.devicePaths[device['path']] != undefined)
rfrappier
2015/07/30 22:21:38
Done.
| |
144 var selectedClass = this.getTextForDeviceClass(device['classValue']); | |
145 device.class = selectedClass; | |
146 this.push('devices', device); | |
147 this.devicePaths[device['path']] = true; | |
148 } | |
149 }, | |
150 | |
151 // Returns the text for the label that corresponds to |classValue|. | |
stevenjb
2015/07/30 18:28:51
@param
rfrappier
2015/07/30 22:21:38
Done.
| |
152 getTextForDeviceClass: function(classValue) { | |
xiyuan
2015/07/30 17:21:22
We probably should consider move this.deviceClassO
rfrappier
2015/07/30 22:21:38
This may be possible, and I agree that this makes
xiyuan
2015/07/30 23:30:37
I don't understand why it would not work with Poly
rfrappier
2015/07/31 00:55:59
The problem is that Polymer's data binding cannot
rfrappier
2015/07/31 01:09:53
Also, I think having this property in the Polyemr
xiyuan
2015/07/31 02:00:00
Why this would not work? Have you tried it? "new B
| |
102 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | 153 for (var i = 0; i < this.deviceClassOptions.length; ++i) { |
103 if (this.deviceClassOptions[i].text == classValue) | 154 if (this.deviceClassOptions[i].value == classValue) |
155 return this.deviceClassOptions[i].text; | |
156 } | |
157 }, | |
158 | |
159 // Returns the integer value which corresponds with the label |classText|. | |
stevenjb
2015/07/30 18:28:51
@param
@return
rfrappier
2015/07/30 22:21:38
Done.
| |
160 getValueForDeviceClass: function(classText) { | |
161 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | |
162 if (this.deviceClassOptions[i].text == classText) | |
104 return this.deviceClassOptions[i].value; | 163 return this.deviceClassOptions[i].value; |
105 } | 164 } |
106 return 0; | 165 return 0; |
107 }, | 166 }, |
108 }); | 167 }); |
OLD | NEW |