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