Disable change log; to remove in future
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / PinCodeActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
20 import java.util.Arrays;
21
22 import com.actionbarsherlock.app.SherlockFragmentActivity;
23
24 import com.owncloud.android.R;
25
26 import android.app.AlertDialog;
27 import android.content.DialogInterface;
28 import android.content.Intent;
29 import android.content.SharedPreferences;
30 import android.os.Bundle;
31 import android.preference.PreferenceManager;
32 import android.text.Editable;
33 import android.text.TextWatcher;
34 import android.view.KeyEvent;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.view.View.OnFocusChangeListener;
38 import android.view.View.OnKeyListener;
39 import android.widget.Button;
40 import android.widget.EditText;
41 import android.widget.TextView;
42
43 public class PinCodeActivity extends SherlockFragmentActivity {
44
45
46 public final static String EXTRA_ACTIVITY = "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
47 public final static String EXTRA_NEW_STATE = "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
48
49 Button bCancel;
50 TextView mPinHdr;
51 TextView mPinHdrExplanation;
52 EditText mText1;
53 EditText mText2;
54 EditText mText3;
55 EditText mText4;
56
57 String [] tempText ={"","","",""};
58
59 String activity;
60
61 boolean confirmingPinCode = false;
62 boolean pinCodeChecked = false;
63 boolean newPasswordEntered = false;
64 boolean bChange = true; // to control that only one blocks jump
65 int tCounter ; // Count the number of attempts an user could introduce the PIN code
66
67
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setContentView(R.layout.pincodelock);
71
72 Intent intent = getIntent();
73 activity = intent.getStringExtra(EXTRA_ACTIVITY);
74
75 bCancel = (Button) findViewById(R.id.cancel);
76 mPinHdr = (TextView) findViewById(R.id.pinHdr);
77 mPinHdrExplanation = (TextView) findViewById(R.id.pinHdrExpl);
78 mText1 = (EditText) findViewById(R.id.txt1);
79 mText1.requestFocus();
80 getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
81 mText2 = (EditText) findViewById(R.id.txt2);
82 mText3 = (EditText) findViewById(R.id.txt3);
83 mText4 = (EditText) findViewById(R.id.txt4);
84
85
86
87 SharedPreferences appPrefs = PreferenceManager
88 .getDefaultSharedPreferences(getApplicationContext());
89
90
91 // Not PIN Code defined yet.
92 // In a previous version settings is allow from start
93 if ( (appPrefs.getString("PrefPinCode1", null) == null ) ){
94 setChangePincodeView(true);
95 pinCodeChecked = true;
96 newPasswordEntered = true;
97
98 }else{
99
100 if (appPrefs.getBoolean("set_pincode", false)){
101 // pincode activated
102 if (activity.equals("preferences")){
103 // PIN has been activated yet
104 mPinHdr.setText(R.string.pincode_configure_your_pin);
105 mPinHdrExplanation.setVisibility(View.VISIBLE);
106 pinCodeChecked = true ; // No need to check it
107 setChangePincodeView(true);
108 }else{
109 // PIN active
110 bCancel.setVisibility(View.INVISIBLE);
111 bCancel.setVisibility(View.GONE);
112 mPinHdr.setText(R.string.pincode_enter_pin_code);
113 mPinHdrExplanation.setVisibility(View.INVISIBLE);
114 setChangePincodeView(false);
115 }
116
117 }else {
118 // pincode removal
119 mPinHdr.setText(R.string.pincode_remove_your_pincode);
120 mPinHdrExplanation.setVisibility(View.INVISIBLE);
121 pinCodeChecked = false;
122 setChangePincodeView(true);
123 }
124
125 }
126 setTextListeners();
127
128
129 }
130
131
132
133 protected void setInitVars(){
134 confirmingPinCode = false;
135 pinCodeChecked = false;
136 newPasswordEntered = false;
137
138 }
139
140 protected void setInitView(){
141 bCancel.setVisibility(View.INVISIBLE);
142 bCancel.setVisibility(View.GONE);
143 mPinHdr.setText(R.string.pincode_enter_pin_code);
144 mPinHdrExplanation.setVisibility(View.INVISIBLE);
145 }
146
147
148 protected void setChangePincodeView(boolean state){
149
150 if(state){
151 bCancel.setVisibility(View.VISIBLE);
152 bCancel.setOnClickListener(new OnClickListener() {
153 @Override
154 public void onClick(View v) {
155
156 SharedPreferences.Editor appPrefsE = PreferenceManager
157 .getDefaultSharedPreferences(getApplicationContext()).edit();
158
159 SharedPreferences appPrefs = PreferenceManager
160 .getDefaultSharedPreferences(getApplicationContext());
161
162 boolean state = appPrefs.getBoolean("set_pincode", false);
163 appPrefsE.putBoolean("set_pincode",!state);
164 appPrefsE.commit();
165 setInitVars();
166 finish();
167 }
168 });
169 }
170
171 }
172
173
174
175 /*
176 *
177 */
178 protected void setTextListeners(){
179
180 /*------------------------------------------------
181 * FIRST BOX
182 -------------------------------------------------*/
183
184 mText1.addTextChangedListener(new TextWatcher() {
185
186 @Override
187 public void onTextChanged(CharSequence s, int start, int before,
188 int count) {
189 }
190
191 @Override
192 public void beforeTextChanged(CharSequence s, int start, int count,
193 int after) {
194 }
195
196 @Override
197 public void afterTextChanged(Editable s) {
198 if (s.length() > 0) {
199 if (!confirmingPinCode){
200 tempText[0] = mText1.getText().toString();
201
202 }
203 mText2.requestFocus();
204 }
205 }
206 });
207
208
209
210 /*------------------------------------------------
211 * SECOND BOX
212 -------------------------------------------------*/
213 mText2.addTextChangedListener(new TextWatcher() {
214
215 @Override
216 public void onTextChanged(CharSequence s, int start, int before,
217 int count) {
218 }
219
220 @Override
221 public void beforeTextChanged(CharSequence s, int start, int count,
222 int after) {
223 }
224
225 @Override
226 public void afterTextChanged(Editable s) {
227 if (s.length() > 0) {
228 if (!confirmingPinCode){
229 tempText[1] = mText2.getText().toString();
230 }
231
232 mText3.requestFocus();
233 }
234 }
235 });
236
237 mText2.setOnKeyListener(new OnKeyListener() {
238
239 @Override
240 public boolean onKey(View v, int keyCode, KeyEvent event) {
241 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
242
243 mText1.setText("");
244 mText1.requestFocus();
245 if (!confirmingPinCode)
246 tempText[0] = "";
247 bChange= false;
248
249 }else if(!bChange){
250 bChange=true;
251
252 }
253 return false;
254 }
255 });
256
257 mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
258
259 @Override
260 public void onFocusChange(View v, boolean hasFocus) {
261 mText2.setCursorVisible(true);
262 if (mText1.getText().toString().equals("")){
263 mText2.setSelected(false);
264 mText2.setCursorVisible(false);
265 mText1.requestFocus();
266 mText1.setSelected(true);
267 mText1.setSelection(0);
268 }
269
270 }
271 });
272
273
274 /*------------------------------------------------
275 * THIRD BOX
276 -------------------------------------------------*/
277 mText3.addTextChangedListener(new TextWatcher() {
278
279 @Override
280 public void onTextChanged(CharSequence s, int start, int before,
281 int count) {
282 }
283
284 @Override
285 public void beforeTextChanged(CharSequence s, int start, int count,
286 int after) {
287 }
288
289 @Override
290 public void afterTextChanged(Editable s) {
291 if (s.length() > 0) {
292 if (!confirmingPinCode){
293 tempText[2] = mText3.getText().toString();
294 }
295 mText4.requestFocus();
296 }
297 }
298 });
299
300 mText3.setOnKeyListener(new OnKeyListener() {
301
302 @Override
303 public boolean onKey(View v, int keyCode, KeyEvent event) {
304 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
305 mText2.requestFocus();
306 if (!confirmingPinCode)
307 tempText[1] = "";
308 mText2.setText("");
309 bChange= false;
310
311 }else if(!bChange){
312 bChange=true;
313
314 }
315 return false;
316 }
317 });
318
319 mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
320
321 @Override
322 public void onFocusChange(View v, boolean hasFocus) {
323 mText3.setCursorVisible(true);
324 if (mText1.getText().toString().equals("")){
325 mText3.setSelected(false);
326 mText3.setCursorVisible(false);
327 mText1.requestFocus();
328 mText1.setSelected(true);
329 mText1.setSelection(0);
330 }else if (mText2.getText().toString().equals("")){
331 mText3.setSelected(false);
332 mText3.setCursorVisible(false);
333 mText2.requestFocus();
334 mText2.setSelected(true);
335 mText2.setSelection(0);
336 }
337
338 }
339 });
340
341 /*------------------------------------------------
342 * FOURTH BOX
343 -------------------------------------------------*/
344 mText4.addTextChangedListener(new TextWatcher() {
345
346 @Override
347 public void onTextChanged(CharSequence s, int start, int before,
348 int count) {
349 }
350
351 @Override
352 public void beforeTextChanged(CharSequence s, int start, int count,
353 int after) {
354 }
355
356 @Override
357 public void afterTextChanged(Editable s) {
358 if (s.length() > 0) {
359
360 if (!confirmingPinCode){
361 tempText[3] = mText4.getText().toString();
362 }
363 mText1.requestFocus();
364
365 if (!pinCodeChecked){
366 pinCodeChecked = checkPincode();
367 }
368
369 if (pinCodeChecked && activity.equals("FileDisplayActivity")){
370 finish();
371 } else if (pinCodeChecked){
372
373 Intent intent = getIntent();
374 String newState = intent.getStringExtra(EXTRA_NEW_STATE);
375
376 if (newState.equals("false")){
377 SharedPreferences.Editor appPrefs = PreferenceManager
378 .getDefaultSharedPreferences(getApplicationContext()).edit();
379 appPrefs.putBoolean("set_pincode",false);
380 appPrefs.commit();
381
382 setInitVars();
383 pinCodeEnd(false);
384
385 }else{
386
387 if (!confirmingPinCode){
388 pinCodeChangeRequest();
389
390 } else {
391 confirmPincode();
392 }
393 }
394
395
396 }
397 }
398 }
399 });
400
401
402
403 mText4.setOnKeyListener(new OnKeyListener() {
404
405 @Override
406 public boolean onKey(View v, int keyCode, KeyEvent event) {
407 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
408 mText3.requestFocus();
409 if (!confirmingPinCode)
410 tempText[2]="";
411 mText3.setText("");
412 bChange= false;
413
414 }else if(!bChange){
415 bChange=true;
416 }
417 return false;
418 }
419 });
420
421 mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
422
423 @Override
424 public void onFocusChange(View v, boolean hasFocus) {
425 mText4.setCursorVisible(true);
426
427 if (mText1.getText().toString().equals("")){
428 mText4.setSelected(false);
429 mText4.setCursorVisible(false);
430 mText1.requestFocus();
431 mText1.setSelected(true);
432 mText1.setSelection(0);
433 }else if (mText2.getText().toString().equals("")){
434 mText4.setSelected(false);
435 mText4.setCursorVisible(false);
436 mText2.requestFocus();
437 mText2.setSelected(true);
438 mText2.setSelection(0);
439 }else if (mText3.getText().toString().equals("")){
440 mText4.setSelected(false);
441 mText4.setCursorVisible(false);
442 mText3.requestFocus();
443 mText3.setSelected(true);
444 mText3.setSelection(0);
445 }
446
447 }
448 });
449
450
451
452 } // end setTextListener
453
454
455 protected void pinCodeChangeRequest(){
456
457 clearBoxes();
458 mPinHdr.setText(R.string.pincode_reenter_your_pincode);
459 mPinHdrExplanation.setVisibility(View.INVISIBLE);
460 confirmingPinCode =true;
461
462 }
463
464
465 protected boolean checkPincode(){
466
467
468 SharedPreferences appPrefs = PreferenceManager
469 .getDefaultSharedPreferences(getApplicationContext());
470
471 String pText1 = appPrefs.getString("PrefPinCode1", null);
472 String pText2 = appPrefs.getString("PrefPinCode2", null);
473 String pText3 = appPrefs.getString("PrefPinCode3", null);
474 String pText4 = appPrefs.getString("PrefPinCode4", null);
475
476 if ( tempText[0].equals(pText1) &&
477 tempText[1].equals(pText2) &&
478 tempText[2].equals(pText3) &&
479 tempText[3].equals(pText4) ) {
480
481 return true;
482
483
484 }else {
485 Arrays.fill(tempText, null);
486 AlertDialog aDialog = new AlertDialog.Builder(this).create();
487 CharSequence errorSeq = getString(R.string.common_error);
488 aDialog.setTitle(errorSeq);
489 CharSequence cseq = getString(R.string.pincode_wrong);
490 aDialog.setMessage(cseq);
491 CharSequence okSeq = getString(R.string.common_ok);
492 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
493
494 @Override
495 public void onClick(DialogInterface dialog, int which) {
496 return;
497 }
498
499 });
500 aDialog.show();
501 clearBoxes();
502 mPinHdr.setText(R.string.pincode_enter_pin_code);
503 mPinHdrExplanation.setVisibility(View.INVISIBLE);
504 newPasswordEntered = true;
505 confirmingPinCode = false;
506
507 }
508
509
510 return false;
511 }
512
513 protected void confirmPincode(){
514
515 confirmingPinCode = false;
516
517 String rText1 = mText1.getText().toString();
518 String rText2 = mText2.getText().toString();
519 String rText3 = mText3.getText().toString();
520 String rText4 = mText4.getText().toString();
521
522 if ( tempText[0].equals(rText1) &&
523 tempText[1].equals(rText2) &&
524 tempText[2].equals(rText3) &&
525 tempText[3].equals(rText4) ) {
526
527 savePincodeAndExit();
528
529 } else {
530
531 Arrays.fill(tempText, null);
532 AlertDialog aDialog = new AlertDialog.Builder(this).create();
533 CharSequence errorSeq = getString(R.string.common_error);
534 aDialog.setTitle(errorSeq);
535 CharSequence cseq = getString(R.string.pincode_mismatch);
536 aDialog.setMessage(cseq);
537 CharSequence okSeq = getString(R.string.common_ok);
538 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
539
540 @Override
541 public void onClick(DialogInterface dialog, int which) {
542 return;
543 }
544
545 });
546 aDialog.show();
547 mPinHdr.setText(R.string.pincode_configure_your_pin);
548 mPinHdrExplanation.setVisibility(View.VISIBLE);
549 clearBoxes();
550 }
551
552 }
553
554
555 protected void pinCodeEnd(boolean state){
556 AlertDialog aDialog = new AlertDialog.Builder(this).create();
557
558 if (state){
559 CharSequence saveSeq = getString(R.string.common_save_exit);
560 aDialog.setTitle(saveSeq);
561 CharSequence cseq = getString(R.string.pincode_stored);
562 aDialog.setMessage(cseq);
563
564 }else{
565 CharSequence saveSeq = getString(R.string.common_save_exit);
566 aDialog.setTitle(saveSeq);
567 CharSequence cseq = getString(R.string.pincode_removed);
568 aDialog.setMessage(cseq);
569
570 }
571 CharSequence okSeq = getString(R.string.common_ok);
572 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
573
574 @Override
575 public void onClick(DialogInterface dialog, int which) {
576 finish();
577 return;
578 }
579
580 });
581 aDialog.show();
582 }
583
584 protected void savePincodeAndExit(){
585 SharedPreferences.Editor appPrefs = PreferenceManager
586 .getDefaultSharedPreferences(getApplicationContext()).edit();
587
588 appPrefs.putString("PrefPinCode1", tempText[0]);
589 appPrefs.putString("PrefPinCode2",tempText[1]);
590 appPrefs.putString("PrefPinCode3", tempText[2]);
591 appPrefs.putString("PrefPinCode4", tempText[3]);
592 appPrefs.putBoolean("set_pincode",true);
593 appPrefs.commit();
594
595 pinCodeEnd(true);
596
597
598
599 }
600
601
602 protected void clearBoxes(){
603
604 mText1.setText("");
605 mText2.setText("");
606 mText3.setText("");
607 mText4.setText("");
608 mText1.requestFocus();
609 }
610
611
612 @Override
613 public boolean onKeyDown(int keyCode, KeyEvent event){
614 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
615
616 if (activity.equals("preferences")){
617 SharedPreferences.Editor appPrefsE = PreferenceManager
618
619 .getDefaultSharedPreferences(getApplicationContext()).edit();
620
621 SharedPreferences appPrefs = PreferenceManager
622 .getDefaultSharedPreferences(getApplicationContext());
623
624 boolean state = appPrefs.getBoolean("set_pincode", false);
625 appPrefsE.putBoolean("set_pincode",!state);
626 appPrefsE.commit();
627 setInitVars();
628 finish();
629 }
630 return true;
631
632 }
633
634 return super.onKeyDown(keyCode, event);
635 }
636
637
638
639
640
641 }