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