5fd7213ac5e04db48b5904fa9ede5ed841578d5c
[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 setChangePincodeView(true);
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 }
191
192 @Override
193 public void beforeTextChanged(CharSequence s, int start, int count,
194 int after) {
195 }
196
197 @Override
198 public void afterTextChanged(Editable s) {
199 if (s.length() > 0) {
200 if (!confirmingPinCode){
201 tempText[0] = mText1.getText().toString();
202
203 }
204 mText2.requestFocus();
205 }
206 }
207 });
208
209
210
211 /*------------------------------------------------
212 * SECOND BOX
213 -------------------------------------------------*/
214 mText2.addTextChangedListener(new TextWatcher() {
215
216 @Override
217 public void onTextChanged(CharSequence s, int start, int before,
218 int count) {
219 }
220
221 @Override
222 public void beforeTextChanged(CharSequence s, int start, int count,
223 int after) {
224 }
225
226 @Override
227 public void afterTextChanged(Editable s) {
228 if (s.length() > 0) {
229 if (!confirmingPinCode){
230 tempText[1] = mText2.getText().toString();
231 }
232
233 mText3.requestFocus();
234 }
235 }
236 });
237
238 mText2.setOnKeyListener(new OnKeyListener() {
239
240 @Override
241 public boolean onKey(View v, int keyCode, KeyEvent event) {
242 // TODO Auto-generated method stub
243
244 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
245
246 mText1.setText("");
247 mText1.requestFocus();
248 if (!confirmingPinCode)
249 tempText[0] = "";
250 bChange= false;
251
252 }else if(!bChange){
253 bChange=true;
254
255 }
256 return false;
257 }
258 });
259
260 mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
261
262 @Override
263 public void onFocusChange(View v, boolean hasFocus) {
264 // TODO Auto-generated method stub
265
266 mText2.setCursorVisible(true);
267 if (mText1.getText().toString().equals("")){
268 mText2.setSelected(false);
269 mText2.setCursorVisible(false);
270 mText1.requestFocus();
271 mText1.setSelected(true);
272 mText1.setSelection(0);
273 }
274
275 }
276 });
277
278
279 /*------------------------------------------------
280 * THIRD BOX
281 -------------------------------------------------*/
282 mText3.addTextChangedListener(new TextWatcher() {
283
284 @Override
285 public void onTextChanged(CharSequence s, int start, int before,
286 int count) {
287 }
288
289 @Override
290 public void beforeTextChanged(CharSequence s, int start, int count,
291 int after) {
292 }
293
294 @Override
295 public void afterTextChanged(Editable s) {
296 if (s.length() > 0) {
297 if (!confirmingPinCode){
298 tempText[2] = mText3.getText().toString();
299 }
300 mText4.requestFocus();
301 }
302 }
303 });
304
305 mText3.setOnKeyListener(new OnKeyListener() {
306
307 @Override
308 public boolean onKey(View v, int keyCode, KeyEvent event) {
309 // TODO Auto-generated method stub
310
311 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
312 mText2.requestFocus();
313 if (!confirmingPinCode)
314 tempText[1] = "";
315 mText2.setText("");
316 bChange= false;
317
318 }else if(!bChange){
319 bChange=true;
320
321 }
322 return false;
323 }
324 });
325
326 mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
327
328 @Override
329 public void onFocusChange(View v, boolean hasFocus) {
330 // TODO Auto-generated method stub
331 mText3.setCursorVisible(true);
332 if (mText1.getText().toString().equals("")){
333 mText3.setSelected(false);
334 mText3.setCursorVisible(false);
335 mText1.requestFocus();
336 mText1.setSelected(true);
337 mText1.setSelection(0);
338 }else if (mText2.getText().toString().equals("")){
339 mText3.setSelected(false);
340 mText3.setCursorVisible(false);
341 mText2.requestFocus();
342 mText2.setSelected(true);
343 mText2.setSelection(0);
344 }
345
346 }
347 });
348
349 /*------------------------------------------------
350 * FOURTH BOX
351 -------------------------------------------------*/
352 mText4.addTextChangedListener(new TextWatcher() {
353
354 @Override
355 public void onTextChanged(CharSequence s, int start, int before,
356 int count) {
357 }
358
359 @Override
360 public void beforeTextChanged(CharSequence s, int start, int count,
361 int after) {
362 }
363
364 @Override
365 public void afterTextChanged(Editable s) {
366 if (s.length() > 0) {
367
368 if (!confirmingPinCode){
369 tempText[3] = mText4.getText().toString();
370 }
371 mText1.requestFocus();
372
373 if (!pinCodeChecked){
374 pinCodeChecked = checkPincode();
375 }
376
377 if (pinCodeChecked && activity.equals("FileDisplayActivity")){
378 finish();
379 } else if (pinCodeChecked){
380
381 Intent intent = getIntent();
382 String newState = intent.getStringExtra(EXTRA_NEW_STATE);
383
384 if (newState.equals("false")){
385 SharedPreferences.Editor appPrefs = PreferenceManager
386 .getDefaultSharedPreferences(getApplicationContext()).edit();
387 appPrefs.putBoolean("set_pincode",false);
388 appPrefs.commit();
389
390 setInitVars();
391 pinCodeEnd(false);
392
393 }else{
394
395 if (!confirmingPinCode){
396 pinCodeChangeRequest();
397
398 } else {
399 confirmPincode();
400 }
401 }
402
403
404 }
405 }
406 }
407 });
408
409
410
411 mText4.setOnKeyListener(new OnKeyListener() {
412
413 @Override
414 public boolean onKey(View v, int keyCode, KeyEvent event) {
415 // TODO Auto-generated method stub
416
417 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
418 mText3.requestFocus();
419 if (!confirmingPinCode)
420 tempText[2]="";
421 mText3.setText("");
422 bChange= false;
423
424 }else if(!bChange){
425 bChange=true;
426 }
427 return false;
428 }
429 });
430
431 mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
432
433 @Override
434 public void onFocusChange(View v, boolean hasFocus) {
435 // TODO Auto-generated method stub
436
437 mText4.setCursorVisible(true);
438
439 if (mText1.getText().toString().equals("")){
440 mText4.setSelected(false);
441 mText4.setCursorVisible(false);
442 mText1.requestFocus();
443 mText1.setSelected(true);
444 mText1.setSelection(0);
445 }else if (mText2.getText().toString().equals("")){
446 mText4.setSelected(false);
447 mText4.setCursorVisible(false);
448 mText2.requestFocus();
449 mText2.setSelected(true);
450 mText2.setSelection(0);
451 }else if (mText3.getText().toString().equals("")){
452 mText4.setSelected(false);
453 mText4.setCursorVisible(false);
454 mText3.requestFocus();
455 mText3.setSelected(true);
456 mText3.setSelection(0);
457 }
458
459 }
460 });
461
462
463
464 } // end setTextListener
465
466
467 protected void pinCodeChangeRequest(){
468
469 clearBoxes();
470 mPinHdr.setText(R.string.pincode_reenter_your_pincode);
471 confirmingPinCode =true;
472
473 }
474
475
476 protected boolean checkPincode(){
477
478
479 SharedPreferences appPrefs = PreferenceManager
480 .getDefaultSharedPreferences(getApplicationContext());
481
482 String pText1 = appPrefs.getString("PrefPinCode1", null);
483 String pText2 = appPrefs.getString("PrefPinCode2", null);
484 String pText3 = appPrefs.getString("PrefPinCode3", null);
485 String pText4 = appPrefs.getString("PrefPinCode4", null);
486
487 if ( tempText[0].equals(pText1) &&
488 tempText[1].equals(pText2) &&
489 tempText[2].equals(pText3) &&
490 tempText[3].equals(pText4) ) {
491
492 return true;
493
494
495 }else {
496 Arrays.fill(tempText, null);
497 AlertDialog aDialog = new AlertDialog.Builder(this).create();
498 CharSequence errorSeq = getString(R.string.common_error);
499 aDialog.setTitle(errorSeq);
500 CharSequence cseq = getString(R.string.pincode_wrong);
501 aDialog.setMessage(cseq);
502 CharSequence okSeq = getString(R.string.common_ok);
503 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
504
505 @Override
506 public void onClick(DialogInterface dialog, int which) {
507 // TODO Auto-generated method stub("");
508 return;
509 }
510
511 });
512 aDialog.show();
513 clearBoxes();
514 mPinHdr.setText(R.string.pincode_enter_pin_code);
515 newPasswordEntered = true;
516 confirmingPinCode = false;
517
518 }
519
520
521 return false;
522 }
523
524 protected void confirmPincode(){
525
526 confirmingPinCode = false;
527
528 String rText1 = mText1.getText().toString();
529 String rText2 = mText2.getText().toString();
530 String rText3 = mText3.getText().toString();
531 String rText4 = mText4.getText().toString();
532
533 if ( tempText[0].equals(rText1) &&
534 tempText[1].equals(rText2) &&
535 tempText[2].equals(rText3) &&
536 tempText[3].equals(rText4) ) {
537
538 savePincodeAndExit();
539
540 } else {
541
542 Arrays.fill(tempText, null);
543 AlertDialog aDialog = new AlertDialog.Builder(this).create();
544 CharSequence errorSeq = getString(R.string.common_error);
545 aDialog.setTitle(errorSeq);
546 CharSequence cseq = getString(R.string.pincode_mismatch);
547 aDialog.setMessage(cseq);
548 CharSequence okSeq = getString(R.string.common_ok);
549 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
550
551 @Override
552 public void onClick(DialogInterface dialog, int which) {
553 // TODO Auto-generated method stub("");
554 return;
555 }
556
557 });
558 aDialog.show();
559 mPinHdr.setText(R.string.pincode_configure_your_pin);
560 clearBoxes();
561 }
562
563 }
564
565
566 protected void pinCodeEnd(boolean state){
567 AlertDialog aDialog = new AlertDialog.Builder(this).create();
568
569 if (state){
570 CharSequence saveSeq = getString(R.string.common_save_exit);
571 aDialog.setTitle(saveSeq);
572 CharSequence cseq = getString(R.string.pincode_stored);
573 aDialog.setMessage(cseq);
574
575 }else{
576 CharSequence saveSeq = getString(R.string.common_save_exit);
577 aDialog.setTitle(saveSeq);
578 CharSequence cseq = getString(R.string.pincode_removed);
579 aDialog.setMessage(cseq);
580
581 }
582 CharSequence okSeq = getString(R.string.common_ok);
583 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
584
585 @Override
586 public void onClick(DialogInterface dialog, int which) {
587 // TODO Auto-generated method stub("");
588 finish();
589 return;
590 }
591
592 });
593 aDialog.show();
594 }
595
596 protected void savePincodeAndExit(){
597 SharedPreferences.Editor appPrefs = PreferenceManager
598 .getDefaultSharedPreferences(getApplicationContext()).edit();
599
600 appPrefs.putString("PrefPinCode1", tempText[0]);
601 appPrefs.putString("PrefPinCode2",tempText[1]);
602 appPrefs.putString("PrefPinCode3", tempText[2]);
603 appPrefs.putString("PrefPinCode4", tempText[3]);
604 appPrefs.putBoolean("set_pincode",true);
605 appPrefs.commit();
606
607 pinCodeEnd(true);
608
609
610
611 }
612
613
614 protected void clearBoxes(){
615
616 mText1.setText("");
617 mText2.setText("");
618 mText3.setText("");
619 mText4.setText("");
620 mText1.requestFocus();
621 }
622
623
624 @Override
625 public boolean onKeyDown(int keyCode, KeyEvent event){
626 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
627
628 if (activity.equals("preferences")){
629 SharedPreferences.Editor appPrefsE = PreferenceManager
630
631 .getDefaultSharedPreferences(getApplicationContext()).edit();
632
633 SharedPreferences appPrefs = PreferenceManager
634 .getDefaultSharedPreferences(getApplicationContext());
635
636 boolean state = appPrefs.getBoolean("set_pincode", false);
637 appPrefsE.putBoolean("set_pincode",!state);
638 appPrefsE.commit();
639 setInitVars();
640 finish();
641 }
642 return true;
643
644 }
645
646 return super.onKeyDown(keyCode, event);
647 }
648
649
650
651
652
653 }