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.
6 * @fileoverview Polymer element for displaying and modifying a list of cellular
14 INCORRECT_PIN
: 'incorrect-pin',
15 INCORRECT_PUK
: 'incorrect-puk',
16 MISMATCHED_PIN
: 'mismatched-pin',
17 INVALID_PIN
: 'invalid-pin',
18 INVALID_PUK
: 'invalid-puk'
21 var PIN_MIN_LENGTH
= 4;
22 var PUK_MIN_LENGTH
= 8;
25 is
: 'network-siminfo',
29 * The network state associated with the element.
30 * @type {?CrOnc.NetworkStateProperties}
35 observer
: 'networkStateChanged_'
38 /** Set to true when a PUK is required to unlock the SIM. */
42 observer
: 'pukRequiredChanged_'
46 * Set to an ErrorType value after an incorrect PIN or PUK entry.
55 sendSimLockEnabled_
: false,
57 /** Polymer networkState changed method. */
58 networkStateChanged_: function() {
59 if (!this.networkState
|| !this.networkState
.Cellular
)
61 var simLockStatus
= /** @type {CrOnc.SIMLockStatus|undefined} */(
62 CrOnc
.getActiveValue(this.networkState
, 'Cellular.SIMLockStatus'));
64 !!simLockStatus
&& simLockStatus
.LockType
== CrOnc
.LockType
.PUK
;
67 /** Polymer networkState changed method. */
68 pukRequiredChanged_: function() {
69 if (this.$.unlockPukDialog
.opened
) {
71 this.$.unlockPuk
.focus();
73 this.$.unlockPukDialog
.close();
77 if (!this.pukRequired
)
80 // If the PUK was activated while attempting to enter or change a pin,
81 // close the dialog and open the unlock PUK dialog.
82 var showUnlockPuk
= false;
83 if (this.$.enterPinDialog
.opened
) {
84 this.$.enterPinDialog
.close();
87 if (this.$.changePinDialog
.opened
) {
88 this.$.changePinDialog
.close();
91 if (this.$.unlockPinDialog
.opened
) {
92 this.$.unlockPinDialog
.close();
98 this.error
= ErrorType
.NONE
;
99 this.$.unlockPukDialog
.open();
100 this.$.unlockPuk
.focus();
103 /** Polymer networkState changed method. */
104 onSimLockEnabledChange_: function(event
) {
105 if (!this.networkState
|| !this.networkState
.Cellular
)
107 this.sendSimLockEnabled_
= event
.target
.checked
;
108 this.error
= ErrorType
.NONE
;
109 this.$.enterPinDialog
.open();
113 * Focuses the correct element when the dialog is opened.
114 * @param {Event} event
117 onEnterPinDialogOpened_: function(event
) {
118 this.$.enterPin
.value
= '';
119 this.$.enterPin
.focus();
123 * Sends the PIN value from the Enter PIN dialog.
124 * @param {Event} event
127 sendEnterPin_: function(event
) {
128 var guid
= this.networkState
&& this.networkState
.GUID
;
132 var pin
= this.$.enterPin
.value
;
133 if (!this.validatePin_(pin
))
136 var simState
= /** @type {!CrOnc.CellularSimState} */({
138 requirePin
: this.sendSimLockEnabled_
140 chrome
.networkingPrivate
.setCellularSimState(guid
, simState
, function() {
141 if (chrome
.runtime
.lastError
) {
142 this.error
= ErrorType
.INCORRECT_PIN
;
144 this.error
= ErrorType
.NONE
;
145 this.$.enterPinDialog
.close();
151 * Opens the Change PIN dialog.
152 * @param {Event} event
155 onChangePin_: function(event
) {
156 if (!this.networkState
|| !this.networkState
.Cellular
)
158 this.error
= ErrorType
.NONE
;
159 this.$.changePinDialog
.open();
163 * Focuses the correct element when the dialog is opened.
164 * @param {Event} event
167 onChangePinDialogOpened_: function(event
) {
168 this.$.changePinOld
.value
= '';
169 this.$.changePinNew1
.value
= '';
170 this.$.changePinNew2
.value
= '';
171 this.$.changePinOld
.focus();
175 * Sends the old and new PIN values from the Change PIN dialog.
176 * @param {Event} event
179 sendChangePin_: function(event
) {
180 var guid
= this.networkState
&& this.networkState
.GUID
;
184 var newPin
= this.$.changePinNew1
.value
;
185 if (!this.validatePin_(newPin
, this.$.changePinNew2
.value
))
188 var simState
= /** @type {!CrOnc.CellularSimState} */({
190 currentPin
: this.$.changePinOld
.value
,
193 chrome
.networkingPrivate
.setCellularSimState(guid
, simState
, function() {
194 if (chrome
.runtime
.lastError
) {
195 this.error
= ErrorType
.INCORRECT_PIN
;
197 this.error
= ErrorType
.NONE
;
198 this.$.changePinDialog
.close();
204 * Opens the Unlock PIN dialog.
205 * @param {Event} event
208 unlockPin_: function(event
) {
209 this.error
= ErrorType
.NONE
;
210 this.$.unlockPinDialog
.open();
214 * Focuses the correct element when the dialog is opened.
215 * @param {Event} event
218 onUnlockPinDialogOpened_: function(event
) {
219 this.$.unlockPin
.value
= '';
220 this.$.unlockPin
.focus();
224 * Sends the PIN value from the Unlock PIN dialog.
225 * @param {Event} event
228 sendUnlockPin_: function(event
) {
229 var guid
= this.networkState
&& this.networkState
.GUID
;
232 var pin
= this.$.unlockPin
.value
;
233 if (!this.validatePin_(pin
))
236 chrome
.networkingPrivate
.unlockCellularSim(guid
, pin
, '', function() {
237 if (chrome
.runtime
.lastError
) {
238 this.error
= ErrorType
.INCORRECT_PIN
;
240 this.error
= ErrorType
.NONE
;
241 this.$.unlockPinDialog
.close();
247 * Opens the Unlock PUK dialog.
248 * @param {Event} event
251 unlockPuk_: function(event
) {
252 this.error
= ErrorType
.NONE
;
253 this.$.unlockPukDialog
.open();
257 * Focuses the correct element when the dialog is opened.
258 * @param {Event} event
261 onUnlockPukDialogOpened_: function(event
) {
262 this.$.unlockPuk
.value
= '';
263 this.$.unlockPin1
.value
= '';
264 this.$.unlockPin2
.value
= '';
265 this.$.unlockPuk
.focus();
269 * Sends the PUK value and new PIN value from the Unblock PUK dialog.
270 * @param {Event} event
273 sendUnlockPuk_: function(event
) {
274 var guid
= this.networkState
&& this.networkState
.GUID
;
278 var puk
= this.$.unlockPuk
.value
;
279 if (!this.validatePuk_(puk
))
281 var pin
= this.$.unlockPin1
.value
;
282 if (!this.validatePin_(pin
, this.$.unlockPin2
.value
))
285 chrome
.networkingPrivate
.unlockCellularSim(guid
, pin
, puk
, function() {
286 if (chrome
.runtime
.lastError
) {
287 this.error
= ErrorType
.INCORRECT_PUK
;
289 this.error
= ErrorType
.NONE
;
290 this.$.unlockSimDialog
.close();
296 * @param {?CrOnc.NetworkStateProperties} state
297 * @return {boolean} True if the Cellular SIM is locked.
300 isSimLocked_: function(state
) {
301 return !!state
&& CrOnc
.isSimLocked(state
);
305 * @param {?CrOnc.NetworkStateProperties} state
306 * @return {string} The message for the number of retries left.
309 getRetriesLeftMsg_: function(state
) {
311 this.get('Cellular.SIMLockStatus.RetriesLeft', state
) || 0;
312 // TODO(stevenjb): Localize
313 return 'Retries left: ' + retriesLeft
.toString();
317 * @param {string} error
318 * @return {boolean} True if an error message should be shown for |error|.
321 showError_: function(error
) {
322 return !!error
&& error
!= ErrorType
.NONE
;
326 * @param {string} error
327 * @return {string} The error message to display for |error|.
330 getErrorMsg_: function(error
) {
331 // TODO(stevenjb_: Translate
332 if (error
== ErrorType
.INCORRECT_PIN
)
333 return 'Incorrect PIN.';
334 if (error
== ErrorType
.INCORRECT_PUK
)
335 return 'Incorrect PUK.';
336 if (error
== ErrorType
.MISMATCHED_PIN
)
337 return 'PIN values do not match.';
338 if (error
== ErrorType
.INVALID_PIN
)
339 return 'Invalid PIN.';
340 if (error
== ErrorType
.INVALID_PUK
)
341 return 'Invalid PUK.';
346 * Checks whether |pin1| is of the proper length and if opt_pin2 is not
347 * undefined, whether pin1 and opt_pin2 match. On any failure, sets
348 * |this.error| and returns false.
349 * @param {string} pin1
350 * @param {string=} opt_pin2
351 * @return {boolean} True if the pins match and are of minimum length.
354 validatePin_: function(pin1
, opt_pin2
) {
355 if (pin1
.length
< PIN_MIN_LENGTH
) {
356 this.error
= ErrorType
.INVALID_PIN
;
359 if (opt_pin2
!= undefined && pin1
!= opt_pin2
) {
360 this.error
= ErrorType
.MISMATCHED_PIN
;
367 * Checks whether |puk| is of the proper length. If not, sets |this.error|
369 * @param {string} puk
370 * @return {boolean} True if the puk is of minimum length.
373 validatePuk_: function(puk
) {
374 if (puk
.length
< PUK_MIN_LENGTH
) {
375 this.error
= ErrorType
.INVALID_PUK
;