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