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
= this.networkState
.Cellular
.SIMLockStatus
;
63 simLockStatus
&& simLockStatus
.LockType
== CrOnc
.LockType
.PUK
;
66 /** Polymer networkState changed method. */
67 pukRequiredChanged_: function() {
68 if (this.$.unlockPukDialog
.opened
) {
70 this.$.unlockPuk
.focus();
72 this.$.unlockPukDialog
.close();
76 if (!this.pukRequired
)
79 // If the PUK was activated while attempting to enter or change a pin,
80 // close the dialog and open the unlock PUK dialog.
81 var showUnlockPuk
= false;
82 if (this.$.enterPinDialog
.opened
) {
83 this.$.enterPinDialog
.close();
86 if (this.$.changePinDialog
.opened
) {
87 this.$.changePinDialog
.close();
90 if (this.$.unlockPinDialog
.opened
) {
91 this.$.unlockPinDialog
.close();
97 this.error
= ErrorType
.NONE
;
98 this.$.unlockPukDialog
.open();
99 this.$.unlockPuk
.focus();
102 /** Polymer networkState changed method. */
103 onSimLockEnabledChange_: function(event
) {
104 if (!this.networkState
|| !this.networkState
.Cellular
)
106 this.sendSimLockEnabled_
= event
.target
.checked
;
107 this.error
= ErrorType
.NONE
;
108 this.$.enterPinDialog
.open();
112 * Focuses the correct element when the dialog is opened.
113 * @param {Event} event
116 onEnterPinDialogOpened_: function(event
) {
117 this.$.enterPin
.value
= '';
118 this.$.enterPin
.focus();
122 * Sends the PIN value from the Enter PIN dialog.
123 * @param {Event} event
126 sendEnterPin_: function(event
) {
127 var guid
= this.networkState
&& this.networkState
.GUID
;
131 var pin
= this.$.enterPin
.value
;
132 if (!this.validatePin_(pin
))
135 var /** @type {?CrOnc.CellularSimState} */ simState
= {
136 requirePin
: this.sendSimLockEnabled_
,
139 chrome
.networkingPrivate
.setCellularSimState(guid
, simState
, function() {
140 if (chrome
.runtime
.lastError
) {
141 this.error
= ErrorType
.INCORRECT_PIN
;
143 this.error
= ErrorType
.NONE
;
144 this.$.enterPinDialog
.close();
150 * Opens the Change PIN dialog.
151 * @param {Event} event
154 onChangePin_: function(event
) {
155 if (!this.networkState
|| !this.networkState
.Cellular
)
157 this.error
= ErrorType
.NONE
;
158 this.$.changePinDialog
.open();
162 * Focuses the correct element when the dialog is opened.
163 * @param {Event} event
166 onChangePinDialogOpened_: function(event
) {
167 this.$.changePinOld
.value
= '';
168 this.$.changePinNew1
.value
= '';
169 this.$.changePinNew2
.value
= '';
170 this.$.changePinOld
.focus();
174 * Sends the old and new PIN values from the Change PIN dialog.
175 * @param {Event} event
178 sendChangePin_: function(event
) {
179 var guid
= this.networkState
&& this.networkState
.GUID
;
183 var newPin
= this.$.changePinNew1
.value
;
184 if (!this.validatePin_(newPin
, this.$.changePinNew2
.value
))
187 var /** @type {?CrOnc.CellularSimState} */ simState
= {
189 currentPin
: this.$.changePinOld
.value
,
192 chrome
.networkingPrivate
.setCellularSimState(guid
, simState
, function() {
193 if (chrome
.runtime
.lastError
) {
194 this.error
= ErrorType
.INCORRECT_PIN
;
196 this.error
= ErrorType
.NONE
;
197 this.$.changePinDialog
.close();
203 * Opens the Unlock PIN dialog.
204 * @param {Event} event
207 unlockPin_: function(event
) {
208 this.error
= ErrorType
.NONE
;
209 this.$.unlockPinDialog
.open();
213 * Focuses the correct element when the dialog is opened.
214 * @param {Event} event
217 onUnlockPinDialogOpened_: function(event
) {
218 this.$.unlockPin
.value
= '';
219 this.$.unlockPin
.focus();
223 * Sends the PIN value from the Unlock PIN dialog.
224 * @param {Event} event
227 sendUnlockPin_: function(event
) {
228 var guid
= this.networkState
&& this.networkState
.GUID
;
231 var pin
= this.$.unlockPin
.value
;
232 if (!this.validatePin_(pin
))
235 chrome
.networkingPrivate
.unlockCellularSim(guid
, pin
, '', function() {
236 if (chrome
.runtime
.lastError
) {
237 this.error
= ErrorType
.INCORRECT_PIN
;
239 this.error
= ErrorType
.NONE
;
240 this.$.unlockPinDialog
.close();
246 * Opens the Unlock PUK dialog.
247 * @param {Event} event
250 unlockPuk_: function(event
) {
251 this.error
= ErrorType
.NONE
;
252 this.$.unlockPukDialog
.open();
256 * Focuses the correct element when the dialog is opened.
257 * @param {Event} event
260 onUnlockPukDialogOpened_: function(event
) {
261 this.$.unlockPuk
.value
= '';
262 this.$.unlockPin1
.value
= '';
263 this.$.unlockPin2
.value
= '';
264 this.$.unlockPuk
.focus();
268 * Sends the PUK value and new PIN value from the Unblock PUK dialog.
269 * @param {Event} event
272 sendUnlockPuk_: function(event
) {
273 var guid
= this.networkState
&& this.networkState
.GUID
;
277 var puk
= this.$.unlockPuk
.value
;
278 if (!this.validatePuk_(puk
))
280 var pin
= this.$.unlockPin1
.value
;
281 if (!this.validatePin_(pin
, this.$.unlockPin2
.value
))
284 chrome
.networkingPrivate
.unlockCellularSim(guid
, pin
, puk
, function() {
285 if (chrome
.runtime
.lastError
) {
286 this.error
= ErrorType
.INCORRECT_PUK
;
288 this.error
= ErrorType
.NONE
;
289 this.$.unlockSimDialog
.close();
295 * @param {?CrOnc.NetworkStateProperties} state
296 * @return {boolean} True if the Cellular SIM is locked.
299 isSimLocked_: function(state
) {
300 return state
&& CrOnc
.isSimLocked(state
);
304 * @param {?CrOnc.NetworkStateProperties} state
305 * @return {string} The message for the number of retries left.
308 getRetriesLeftMsg_: function(state
) {
310 this.get('Cellular.SIMLockStatus.RetriesLeft', state
) || 0;
311 // TODO(stevenjb): Localize
312 return 'Retries left: ' + retriesLeft
.toString();
316 * @param {string} error
317 * @return {boolean} True if an error message should be shown for |error|.
320 showError_: function(error
) {
321 return error
&& error
!= ErrorType
.NONE
;
325 * @param {string} error
326 * @return {string} The error message to display for |error|.
329 getErrorMsg_: function(error
) {
330 // TODO(stevenjb_: Translate
331 if (error
== ErrorType
.INCORRECT_PIN
)
332 return 'Incorrect PIN.';
333 if (error
== ErrorType
.INCORRECT_PUK
)
334 return 'Incorrect PUK.';
335 if (error
== ErrorType
.MISMATCHED_PIN
)
336 return 'PIN values do not match.';
337 if (error
== ErrorType
.INVALID_PIN
)
338 return 'Invalid PIN.';
339 if (error
== ErrorType
.INVALID_PUK
)
340 return 'Invalid PUK.';
345 * Checks whether |pin1| is of the proper length and if pin2 is not
346 * undefined, whether pin1 and pin2 match. On any failure, sets |this.error|
348 * @param {string} pin1
349 * @param {string|undefined} pin2
350 * @return {boolean} True if the pins match and are of minimum length.
353 validatePin_: function(pin1
, pin2
) {
354 if (pin1
.length
< PIN_MIN_LENGTH
) {
355 this.error
= ErrorType
.INVALID_PIN
;
358 if (pin2
!= undefined && pin1
!= pin2
) {
359 this.error
= ErrorType
.MISMATCHED_PIN
;
366 * Checks whether |puk| is of the proper length. If not, sets |this.error|
368 * @param {string} puk
369 * @return {boolean} True if the puk is of minimum length.
372 validatePuk_: function(puk
) {
373 if (puk
.length
< PUK_MIN_LENGTH
) {
374 this.error
= ErrorType
.INVALID_PUK
;