| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * 'settings-bluetooth-pair-device-dialog' is the settings dialog for pairing | |
| 8 * a bluetooth device. | |
| 9 */ | |
| 10 | |
| 11 (function() { | |
| 12 | |
| 13 var PairingEventType = chrome.bluetoothPrivate.PairingEventType; | |
| 14 | |
| 15 Polymer({ | |
| 16 is: 'settings-bluetooth-pair-device-dialog', | |
| 17 | |
| 18 behaviors: [I18nBehavior], | |
| 19 | |
| 20 properties: { | |
| 21 /** | |
| 22 * Current Pairing device. | |
| 23 * @type {?chrome.bluetooth.Device|undefined} | |
| 24 */ | |
| 25 pairingDevice: Object, | |
| 26 | |
| 27 /** | |
| 28 * Current Pairing event. | |
| 29 * @type {?chrome.bluetoothPrivate.PairingEvent|undefined} | |
| 30 */ | |
| 31 pairingEvent: Object, | |
| 32 | |
| 33 /** | |
| 34 * @const | |
| 35 * @type {!Array<number>} | |
| 36 */ | |
| 37 digits: { | |
| 38 type: Array, | |
| 39 readOnly: true, | |
| 40 value: [0, 1, 2, 3, 4, 5], | |
| 41 }, | |
| 42 }, | |
| 43 | |
| 44 observers: [ | |
| 45 'pairingChanged_(pairingDevice, pairingEvent)', | |
| 46 ], | |
| 47 | |
| 48 /** | |
| 49 * @param {?chrome.bluetooth.Device} pairingDevice | |
| 50 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 51 * @private | |
| 52 */ | |
| 53 pairingChanged_: function(pairingDevice, pairingEvent) { | |
| 54 // Auto-close the dialog when pairing completes. | |
| 55 if (pairingDevice && pairingDevice.connected) { | |
| 56 this.fire('close-dialog', ''); | |
| 57 return; | |
| 58 } | |
| 59 }, | |
| 60 | |
| 61 /** | |
| 62 * @param {?chrome.bluetooth.Device} device | |
| 63 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 64 * @return {string} | |
| 65 * @private | |
| 66 */ | |
| 67 getMessage_: function(device, pairingEvent) { | |
| 68 if (!device) | |
| 69 return ''; | |
| 70 var message; | |
| 71 if (!pairingEvent) | |
| 72 message = 'bluetoothStartConnecting'; | |
| 73 else | |
| 74 message = this.getEventDesc_(pairingEvent.pairing); | |
| 75 return this.i18n(message, device.name); | |
| 76 }, | |
| 77 | |
| 78 /** | |
| 79 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 80 * @return {boolean} | |
| 81 * @private | |
| 82 */ | |
| 83 showEnterPincode_: function(pairingEvent) { | |
| 84 return !!pairingEvent && | |
| 85 pairingEvent.pairing == PairingEventType.REQUEST_PINCODE; | |
| 86 }, | |
| 87 | |
| 88 /** | |
| 89 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 90 * @return {boolean} | |
| 91 * @private | |
| 92 */ | |
| 93 showEnterPasskey_: function(pairingEvent) { | |
| 94 return !!pairingEvent && | |
| 95 pairingEvent.pairing == PairingEventType.REQUEST_PASSKEY; | |
| 96 }, | |
| 97 | |
| 98 /** | |
| 99 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 100 * @return {boolean} | |
| 101 * @private | |
| 102 */ | |
| 103 showDisplayPassOrPin_: function(pairingEvent) { | |
| 104 if (!pairingEvent) | |
| 105 return false; | |
| 106 var pairing = pairingEvent.pairing; | |
| 107 return ( | |
| 108 pairing == PairingEventType.DISPLAY_PINCODE || | |
| 109 pairing == PairingEventType.DISPLAY_PASSKEY || | |
| 110 pairing == PairingEventType.CONFIRM_PASSKEY || | |
| 111 pairing == PairingEventType.KEYS_ENTERED); | |
| 112 }, | |
| 113 | |
| 114 /** | |
| 115 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 116 * @return {boolean} | |
| 117 * @private | |
| 118 */ | |
| 119 showAcceptReject_: function(pairingEvent) { | |
| 120 return !!pairingEvent && | |
| 121 pairingEvent.pairing == PairingEventType.CONFIRM_PASSKEY; | |
| 122 }, | |
| 123 | |
| 124 /** | |
| 125 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 126 * @return {boolean} | |
| 127 * @private | |
| 128 */ | |
| 129 showConnect_: function(pairingEvent) { | |
| 130 if (!pairingEvent) | |
| 131 return false; | |
| 132 var pairing = pairingEvent.pairing; | |
| 133 if (pairing == PairingEventType.REQUEST_PINCODE) { | |
| 134 var pincode = /** @type {{invalid: boolean}} */(this.$.pincode); | |
| 135 return !pincode.invalid; | |
| 136 } else if (pairing == PairingEventType.REQUEST_PASSKEY) { | |
| 137 var passkey = /** @type {{invalid: boolean}} */(this.$.passkey); | |
| 138 return !passkey.invalid; | |
| 139 } | |
| 140 return false; | |
| 141 }, | |
| 142 | |
| 143 /** | |
| 144 * @param {?chrome.bluetooth.Device} device | |
| 145 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 146 * @return {boolean} | |
| 147 * @private | |
| 148 */ | |
| 149 showDismiss_: function(device, pairingEvent) { | |
| 150 return (!!device && device.paired) || | |
| 151 (!!pairingEvent && pairingEvent.pairing == PairingEventType.COMPLETE); | |
| 152 }, | |
| 153 | |
| 154 /** @private */ | |
| 155 onAcceptTap_: function() { | |
| 156 this.sendResponse_(chrome.bluetoothPrivate.PairingResponse.CONFIRM); | |
| 157 }, | |
| 158 | |
| 159 /** @private */ | |
| 160 onConnectTap_: function() { | |
| 161 this.sendResponse_(chrome.bluetoothPrivate.PairingResponse.CONFIRM); | |
| 162 }, | |
| 163 | |
| 164 /** @private */ | |
| 165 onRejectTap_: function() { | |
| 166 this.sendResponse_(chrome.bluetoothPrivate.PairingResponse.REJECT); | |
| 167 }, | |
| 168 | |
| 169 /** @private */ | |
| 170 onCancelTap_: function() { | |
| 171 this.sendResponse_(chrome.bluetoothPrivate.PairingResponse.CANCEL); | |
| 172 // Close the dialog immediately. | |
| 173 this.fire('close-dialog', ''); | |
| 174 }, | |
| 175 | |
| 176 /** @private */ | |
| 177 onDismissTap_: function() { this.fire('close-dialog', ''); }, | |
| 178 | |
| 179 /** @private */ | |
| 180 sendResponse_: function(response) { | |
| 181 if (!this.pairingDevice) | |
| 182 return; | |
| 183 var options = | |
| 184 /** @type {!chrome.bluetoothPrivate.SetPairingResponseOptions} */ { | |
| 185 device: this.pairingDevice, | |
| 186 response: response | |
| 187 }; | |
| 188 if (response == chrome.bluetoothPrivate.PairingResponse.CONFIRM) { | |
| 189 var pairing = this.pairingEvent.pairing; | |
| 190 if (pairing == PairingEventType.REQUEST_PINCODE) | |
| 191 options.pincode = this.$.pincode.value; | |
| 192 else if (pairing == PairingEventType.REQUEST_PASSKEY) | |
| 193 options.passkey = parseInt(this.$.passkey.value, 10); | |
| 194 } | |
| 195 this.fire('response', options); | |
| 196 }, | |
| 197 | |
| 198 /** | |
| 199 * @param {!PairingEventType} eventType | |
| 200 * @return {string} | |
| 201 * @private | |
| 202 */ | |
| 203 getEventDesc_: function(eventType) { | |
| 204 assert(eventType); | |
| 205 if (eventType == PairingEventType.COMPLETE || | |
| 206 eventType == PairingEventType.KEYS_ENTERED || | |
| 207 eventType == PairingEventType.REQUEST_AUTHORIZATION) { | |
| 208 return 'bluetoothStartConnecting'; | |
| 209 } | |
| 210 return 'bluetooth_' + /** @type {string} */(eventType); | |
| 211 }, | |
| 212 | |
| 213 /** | |
| 214 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 215 * @param {number} index | |
| 216 * @return {string} | |
| 217 * @private | |
| 218 */ | |
| 219 getPinDigit_: function(pairingEvent, index) { | |
| 220 if (!pairingEvent) | |
| 221 return ''; | |
| 222 var digit = '0'; | |
| 223 var pairing = pairingEvent.pairing; | |
| 224 if (pairing == PairingEventType.DISPLAY_PINCODE && pairingEvent.pincode && | |
| 225 index < pairingEvent.pincode.length) { | |
| 226 digit = pairingEvent.pincode[index]; | |
| 227 } else if (pairingEvent.passkey && | |
| 228 (pairing == PairingEventType.DISPLAY_PASSKEY || | |
| 229 pairing == PairingEventType.KEYS_ENTERED || | |
| 230 pairing == PairingEventType.CONFIRM_PASSKEY)) { | |
| 231 var passkeyString = String(pairingEvent.passkey); | |
| 232 if (index < passkeyString.length) | |
| 233 digit = passkeyString[index]; | |
| 234 } | |
| 235 return digit; | |
| 236 }, | |
| 237 | |
| 238 /** | |
| 239 * @param {?chrome.bluetoothPrivate.PairingEvent} pairingEvent | |
| 240 * @param {number} index | |
| 241 * @return {string} | |
| 242 * @private | |
| 243 */ | |
| 244 getPinClass_: function(pairingEvent, index) { | |
| 245 if (!pairingEvent) | |
| 246 return ''; | |
| 247 if (pairingEvent.pairing == PairingEventType.CONFIRM_PASSKEY) | |
| 248 return 'confirm'; | |
| 249 var cssClass = 'display'; | |
| 250 if (pairingEvent.pairing == PairingEventType.DISPLAY_PASSKEY) { | |
| 251 if (index == 0) | |
| 252 cssClass += ' next'; | |
| 253 else | |
| 254 cssClass += ' untyped'; | |
| 255 } else if ( | |
| 256 pairingEvent.pairing == PairingEventType.KEYS_ENTERED && | |
| 257 pairingEvent.enteredKey) { | |
| 258 var enteredKey = pairingEvent.enteredKey; // 1-7 | |
| 259 var lastKey = this.digits.length; // 6 | |
| 260 if ((index == -1 && enteredKey > lastKey) || (index + 1 == enteredKey)) | |
| 261 cssClass += ' next'; | |
| 262 else if (index > enteredKey) | |
| 263 cssClass += ' untyped'; | |
| 264 } | |
| 265 return cssClass; | |
| 266 }, | |
| 267 }); | |
| 268 })(); | |
| OLD | NEW |