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