1 // Copyright (c) 2012 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.
5 cr.define('mobile', function() {
10 cr.addSingletonGetter(SimUnlock);
12 // State of the dialog.
13 SimUnlock.SIM_UNLOCK_LOADING = -1;
14 SimUnlock.SIM_ABSENT_NOT_LOCKED = 0,
15 SimUnlock.SIM_NOT_LOCKED_ASK_PIN = 1;
16 SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN = 2;
17 SimUnlock.SIM_LOCKED_PIN = 3;
18 SimUnlock.SIM_LOCKED_NO_PIN_TRIES_LEFT = 4;
19 SimUnlock.SIM_LOCKED_PUK = 5;
20 SimUnlock.SIM_LOCKED_NO_PUK_TRIES_LEFT = 6;
21 SimUnlock.SIM_DISABLED = 7;
23 // Mode of the dialog.
24 SimUnlock.SIM_DIALOG_UNLOCK = 0;
25 SimUnlock.SIM_DIALOG_CHANGE_PIN = 1;
26 SimUnlock.SIM_DIALOG_SET_LOCK_ON = 2;
27 SimUnlock.SIM_DIALOG_SET_LOCK_OFF = 3;
30 SimUnlock.ERROR_PIN = 'incorrectPin';
31 SimUnlock.ERROR_PUK = 'incorrectPuk';
32 SimUnlock.ERROR_OK = 'ok';
35 SimUnlock.PIN_MIN_LENGTH = 4;
36 SimUnlock.PUK_LENGTH = 8;
38 SimUnlock.localStrings_ = new LocalStrings();
40 SimUnlock.prototype = {
42 mode_: SimUnlock.SIM_DIALOG_UNLOCK,
45 'width': window.innerWidth,
46 'height': window.innerHeight
48 state_: SimUnlock.SIM_UNLOCK_LOADING,
50 changeState_: function(simInfo) {
51 var newState = simInfo.state;
52 var error = simInfo.error;
53 var tries = simInfo.tries;
57 case SimUnlock.SIM_UNLOCK_LOADING:
59 case SimUnlock.SIM_ABSENT_NOT_LOCKED:
62 case SimUnlock.SIM_LOCKED_PIN:
63 if (error == SimUnlock.ERROR_OK) {
64 pinMessage = SimUnlock.localStrings_.getStringF(
65 'enterPinTriesMessage', tries);
66 $('pin-error-msg').classList.remove('error');
67 } else if (error == SimUnlock.ERROR_PIN) {
68 pinMessage = SimUnlock.localStrings_.getStringF(
69 'incorrectPinTriesMessage', tries);
70 $('pin-error-msg').classList.add('error');
72 $('pin-error-msg').textContent = pinMessage;
73 $('pin-input').value = '';
74 SimUnlock.enablePinDialog(true);
75 $('locked-pin-overlay').hidden = false;
76 $('pin-input').focus();
78 case SimUnlock.SIM_NOT_LOCKED_ASK_PIN:
79 if (error == SimUnlock.ERROR_OK) {
80 pinMessage = SimUnlock.localStrings_.getString('enterPinMessage');
81 $('pin-error-msg').classList.remove('error');
82 } else if (error == SimUnlock.ERROR_PIN) {
83 pinMessage = SimUnlock.localStrings_.getStringF(
84 'incorrectPinTriesMessage', tries);
85 $('pin-error-msg').classList.add('error');
87 $('pin-error-msg').textContent = pinMessage;
88 $('pin-input').value = '';
89 SimUnlock.enablePinDialog(true);
90 $('locked-pin-overlay').hidden = false;
91 $('pin-input').focus();
93 case SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN:
94 SimUnlock.prepareChoosePinDialog(true);
95 if (error == SimUnlock.ERROR_OK) {
96 pinMessage = SimUnlock.localStrings_.getString('changePinMessage');
97 $('choose-pin-msg').classList.remove('error');
98 } else if (error == SimUnlock.ERROR_PIN) {
99 pinMessage = SimUnlock.localStrings_.getStringF(
100 'incorrectPinTriesMessage', tries);
101 $('choose-pin-msg').classList.add('error');
103 $('choose-pin-msg').textContent = pinMessage;
104 $('old-pin-input').value = '';
105 $('new-pin-input').value = '';
106 $('retype-new-pin-input').value = '';
107 $('choose-pin-overlay').hidden = false;
108 SimUnlock.enableChoosePinDialog(true);
109 $('old-pin-input').focus();
111 case SimUnlock.SIM_LOCKED_NO_PIN_TRIES_LEFT:
112 $('locked-pin-no-tries-overlay').hidden = false;
114 case SimUnlock.SIM_LOCKED_PUK:
115 $('puk-input').value = '';
116 if (tries && tries >= 0) {
117 var pukMessage = SimUnlock.localStrings_.getStringF(
118 'enterPukWarning', tries);
119 $('puk-warning-msg').textContent = pukMessage;
121 $('enter-puk-confirm').disabled = true;
122 $('locked-puk-overlay').hidden = false;
123 $('puk-input').focus();
125 // Resize the dialog to accomodate the PUK contents.
126 this.updateDialogSize_(0, 45);
128 case SimUnlock.SIM_LOCKED_NO_PUK_TRIES_LEFT:
129 $('locked-puk-no-tries-overlay').hidden = false;
131 case SimUnlock.SIM_DISABLED:
132 $('sim-disabled-overlay').hidden = false;
135 this.state_ = newState;
139 * Returns current state of the dialog.
140 * @type {number} value of the state.
146 updateDialogSize_: function(deltaX, deltaY) {
147 window.resizeTo(this.defaultDialogSize_.width + deltaX,
148 this.defaultDialogSize_.height + deltaY);
151 hideAll_: function() {
152 $('locked-pin-overlay').hidden = true;
153 $('locked-pin-no-tries-overlay').hidden = true;
154 $('locked-puk-overlay').hidden = true;
155 $('choose-pin-overlay').hidden = true;
156 $('locked-puk-no-tries-overlay').hidden = true;
157 $('sim-disabled-overlay').hidden = true;
160 newPinEntered_: function(newPin, newPin2) {
161 var changePinMode = this.state_ == SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN;
162 if (newPin.length < SimUnlock.PIN_MIN_LENGTH ||
163 newPin2.length < SimUnlock.PIN_MIN_LENGTH ||
165 $('old-pin-input').value.length < SimUnlock.PIN_MIN_LENGTH)) {
168 if (newPin != newPin2) {
169 $('choose-pin-error').hidden = false;
170 $('old-pin-input').value = '';
171 $('new-pin-input').value = '';
172 $('retype-new-pin-input').value = '';
174 $('old-pin-input').focus();
176 $('new-pin-input').focus();
178 $('choose-pin-error').hidden = true;
179 SimUnlock.enableChoosePinDialog(false);
181 var oldPin = $('old-pin-input').value;
182 chrome.send('changePinCode', [oldPin, newPin]);
184 chrome.send('enterPukCode', [this.pukValue_, newPin]);
190 pukEntered_: function(pukValue) {
191 if (pukValue.length < SimUnlock.PUK_LENGTH)
193 this.pukValue_ = pukValue;
195 SimUnlock.prepareChoosePinDialog(false);
196 SimUnlock.enableChoosePinDialog(true);
197 $('new-pin-input').value = '';
198 $('retype-new-pin-input').value = '';
199 $('choose-pin-overlay').hidden = false;
200 $('new-pin-input').focus();
201 this.updateDialogSize_(0, 18);
204 updateSimStatus_: function(simInfo) {
205 this.changeState_(simInfo);
209 SimUnlock.cancel = function() {
210 chrome.send('cancel');
214 SimUnlock.close = function() {
218 SimUnlock.initialize = function() {
219 this.initialized_ = true;
221 var modePattern = /(^\?|&)mode=([^&#]*)/;
222 var results = modePattern.exec(window.location.search);
223 if (results == null) {
224 this.mode_ = SimUnlock.SIM_DIALOG_UNLOCK;
226 var mode = results[2];
227 if (mode == 'change-pin')
228 this.mode_ = SimUnlock.SIM_DIALOG_CHANGE_PIN;
229 else if (mode == 'set-lock-on')
230 this.mode_ = SimUnlock.SIM_DIALOG_SET_LOCK_ON;
231 else if (mode == 'set-lock-off')
232 this.mode_ = SimUnlock.SIM_DIALOG_SET_LOCK_OFF;
236 $('pin-input').addEventListener('keydown', function(event) {
237 if (event.keyIdentifier == 'Enter') {
238 SimUnlock.submitPin();
242 $('pin-input').addEventListener('keyup', function(event) {
243 $('enter-pin-confirm').disabled =
244 $('enter-pin-dismiss').disabled ||
245 this.value.length < SimUnlock.PIN_MIN_LENGTH;
247 $('pin-input').addEventListener('textInput',
248 SimUnlock.processInput.bind(this));
249 $('enter-pin-confirm').addEventListener('click', function(event) {
250 SimUnlock.submitPin();
252 $('enter-pin-dismiss').addEventListener('click', function(event) {
256 // No PIN retries left screen.
257 $('pin-no-tries-proceed').addEventListener('click', function(event) {
258 chrome.send('proceedToPukInput');
260 $('pin-no-tries-dismiss').addEventListener('click', function(event) {
265 $('puk-input').addEventListener('keydown', function(event) {
266 if (event.keyIdentifier == 'Enter') {
267 SimUnlock.pukEntered($('puk-input').value);
271 $('puk-input').addEventListener('keyup', function(event) {
272 $('enter-puk-confirm').disabled =
273 $('enter-puk-dismiss').disabled ||
274 this.value.length < SimUnlock.PUK_LENGTH;
276 $('puk-input').addEventListener('textInput',
277 SimUnlock.processInput.bind(this));
278 $('enter-puk-confirm').addEventListener('click', function(event) {
279 SimUnlock.pukEntered($('puk-input').value);
281 $('enter-puk-dismiss').addEventListener('click', function(event) {
285 // Change PIN / new PIN screen.
286 $('old-pin-input').addEventListener('keydown', function(event) {
287 if (event.keyIdentifier == 'Enter') {
288 if (this.value.length >= SimUnlock.PIN_MIN_LENGTH)
289 $('new-pin-input').focus();
293 $('old-pin-input').addEventListener('keyup', function(event) {
294 $('choose-pin-confirm').disabled =
295 $('choose-pin-dismiss').disabled ||
296 this.value.length < SimUnlock.PIN_MIN_LENGTH ||
297 $('new-pin-input').value.length < SimUnlock.PIN_MIN_LENGTH ||
298 $('retype-new-pin-input').value.length < SimUnlock.PIN_MIN_LENGTH;
300 $('old-pin-input').addEventListener('textInput',
301 SimUnlock.processInput.bind(this));
302 $('new-pin-input').addEventListener('keydown', function(event) {
303 if (event.keyIdentifier == 'Enter') {
304 if (this.value.length >= SimUnlock.PIN_MIN_LENGTH)
305 $('retype-new-pin-input').focus();
309 $('new-pin-input').addEventListener('keyup', function(event) {
311 SimUnlock.getInstance().state == SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN;
313 $('old-pin-input').value.length >= SimUnlock.PIN_MIN_LENGTH;
314 var oldPinOk = !changePinMode || oldPinLengthOk;
315 $('choose-pin-confirm').disabled =
316 $('choose-pin-dismiss').disabled ||
317 this.value.length < SimUnlock.PIN_MIN_LENGTH ||
318 $('retype-new-pin-input').value.length < SimUnlock.PIN_MIN_LENGTH ||
321 $('new-pin-input').addEventListener('textInput',
322 SimUnlock.processInput.bind(this));
323 $('retype-new-pin-input').addEventListener('keyup', function(event) {
325 SimUnlock.getInstance().state == SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN;
327 $('old-pin-input').value.length >= SimUnlock.PIN_MIN_LENGTH;
328 var oldPinOk = !changePinMode || oldPinLengthOk;
329 $('choose-pin-confirm').disabled =
330 $('choose-pin-dismiss').disabled ||
331 this.value.length < SimUnlock.PIN_MIN_LENGTH ||
332 $('new-pin-input').value.length < SimUnlock.PIN_MIN_LENGTH ||
335 $('retype-new-pin-input').addEventListener('keydown', function(event) {
336 if (event.keyIdentifier == 'Enter') {
337 SimUnlock.newPinEntered($('new-pin-input').value,
338 $('retype-new-pin-input').value);
342 $('retype-new-pin-input').addEventListener('textInput',
343 SimUnlock.processInput.bind(this));
344 $('choose-pin-confirm').addEventListener('click', function(event) {
345 SimUnlock.newPinEntered($('new-pin-input').value,
346 $('retype-new-pin-input').value);
348 $('choose-pin-dismiss').addEventListener('click', function(event) {
352 // SIM blocked screens.
353 $('puk-no-tries-confirm').addEventListener('click', function(event) {
356 $('sim-disabled-confirm').addEventListener('click', function(event) {
359 chrome.send('simStatusInitialize', [this.mode_]);
362 SimUnlock.enablePinDialog = function(enabled) {
363 $('pin-input').disabled = !enabled;
364 // Ok button is initially disabled.
365 $('enter-pin-confirm').disabled = true;
366 $('enter-pin-dismiss').disabled = !enabled;
369 SimUnlock.enableChoosePinDialog = function(enabled) {
370 $('old-pin-input').disabled = !enabled;
371 $('new-pin-input').disabled = !enabled;
372 $('retype-new-pin-input').disabled = !enabled;
373 // Ok button is initially disabled.
374 $('choose-pin-confirm').disabled = true;
375 $('choose-pin-dismiss').disabled = !enabled;
378 SimUnlock.submitPin = function() {
379 var pin = $('pin-input').value;
380 if (pin.length < SimUnlock.PIN_MIN_LENGTH)
382 SimUnlock.enablePinDialog(false);
383 chrome.send('enterPinCode', [pin]);
386 SimUnlock.prepareChoosePinDialog = function(changePin) {
387 // Our dialog has different height than choose-pin step of the
388 // unlock process which we're reusing.
390 $('choose-pin-content-area').classList.remove('choose-pin-content-area');
391 $('choose-pin-content-area').classList.add('change-pin-content-area');
392 var title = SimUnlock.localStrings_.getString('changePinTitle');
393 $('choose-pin-title').textContent = title;
395 $('choose-pin-content-area').classList.remove('change-pin-content-area');
396 $('choose-pin-content-area').classList.add('choose-pin-content-area');
397 var pinMessage = SimUnlock.localStrings_.getString('choosePinMessage');
398 $('choose-pin-msg').classList.remove('error');
399 $('choose-pin-msg').textContent = pinMessage;
400 var title = SimUnlock.localStrings_.getString('choosePinTitle');
401 $('choose-pin-title').textContent = title;
403 $('old-pin-label').hidden = !changePin;
404 $('old-pin-input-area').hidden = !changePin;
407 SimUnlock.newPinEntered = function(newPin, newPin2) {
408 SimUnlock.getInstance().newPinEntered_(newPin, newPin2);
411 SimUnlock.pukEntered = function(pukValue) {
412 SimUnlock.getInstance().pukEntered_(pukValue);
416 * Processes PIN/PUK fields input, restricting it to 0..9.
418 SimUnlock.processInput = function(event) {
419 if (/[^0-9]+/gi.test(event.data))
420 event.preventDefault();
423 SimUnlock.simStateChanged = function(simInfo) {
424 SimUnlock.getInstance().updateSimStatus_(simInfo);
434 disableTextSelectAndDrag();