068461a8f0f9dde2ccd957af6aa82d3cfa89c7ad
[pub/Android/ownCloud.git] / src / com / owncloud / 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 com.owncloud.android.ui.activity;
18
19 import java.util.Arrays;
20
21 import com.actionbarsherlock.app.SherlockFragmentActivity;
22 import com.owncloud.android.R;
23 import com.owncloud.android.ui.CustomButton;
24
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.EditText;
40 import android.widget.TextView;
41
42 public class PinCodeActivity extends SherlockFragmentActivity {
43
44
45 public final static String EXTRA_ACTIVITY = "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE = "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
47
48 CustomButton 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 = (CustomButton) 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 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 setChangePincodeView(true);
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 mPinHdrExplanation.setVisibility(View.VISIBLE);
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 mPinHdrExplanation.setVisibility(View.INVISIBLE);
111 setChangePincodeView(false);
112 }
113
114 }else {
115 // pincode removal
116 mPinHdr.setText(R.string.pincode_remove_your_pincode);
117 mPinHdrExplanation.setVisibility(View.INVISIBLE);
118 pinCodeChecked = false;
119 setChangePincodeView(true);
120 }
121
122 }
123 setTextListeners();
124
125
126 }
127
128
129
130 protected void setInitVars(){
131 confirmingPinCode = false;
132 pinCodeChecked = false;
133 newPasswordEntered = false;
134
135 }
136
137 protected void setInitView(){
138 bCancel.setVisibility(View.INVISIBLE);
139 bCancel.setVisibility(View.GONE);
140 mPinHdr.setText(R.string.pincode_enter_pin_code);
141 mPinHdrExplanation.setVisibility(View.INVISIBLE);
142 }
143
144
145 protected void setChangePincodeView(boolean state){
146
147 if(state){
148 bCancel.setVisibility(View.VISIBLE);
149 bCancel.setOnClickListener(new OnClickListener() {
150 @Override
151 public void onClick(View v) {
152
153 SharedPreferences.Editor appPrefsE = PreferenceManager
154 .getDefaultSharedPreferences(getApplicationContext()).edit();
155
156 SharedPreferences appPrefs = PreferenceManager
157 .getDefaultSharedPreferences(getApplicationContext());
158
159 boolean state = appPrefs.getBoolean("set_pincode", false);
160 appPrefsE.putBoolean("set_pincode",!state);
161 appPrefsE.commit();
162 setInitVars();
163 finish();
164 }
165 });
166 }
167
168 }
169
170
171
172 /*
173 *
174 */
175 protected void setTextListeners(){
176
177 /*------------------------------------------------
178 * FIRST BOX
179 -------------------------------------------------*/
180
181 mText1.addTextChangedListener(new TextWatcher() {
182
183 @Override
184 public void onTextChanged(CharSequence s, int start, int before,
185 int count) {
186 }
187
188 @Override
189 public void beforeTextChanged(CharSequence s, int start, int count,
190 int after) {
191 }
192
193 @Override
194 public void afterTextChanged(Editable s) {
195 if (s.length() > 0) {
196 if (!confirmingPinCode){
197 tempText[0] = mText1.getText().toString();
198
199 }
200 mText2.requestFocus();
201 }
202 }
203 });
204
205
206
207 /*------------------------------------------------
208 * SECOND BOX
209 -------------------------------------------------*/
210 mText2.addTextChangedListener(new TextWatcher() {
211
212 @Override
213 public void onTextChanged(CharSequence s, int start, int before,
214 int count) {
215 }
216
217 @Override
218 public void beforeTextChanged(CharSequence s, int start, int count,
219 int after) {
220 }
221
222 @Override
223 public void afterTextChanged(Editable s) {
224 if (s.length() > 0) {
225 if (!confirmingPinCode){
226 tempText[1] = mText2.getText().toString();
227 }
228
229 mText3.requestFocus();
230 }
231 }
232 });
233
234 mText2.setOnKeyListener(new OnKeyListener() {
235
236 @Override
237 public boolean onKey(View v, int keyCode, KeyEvent event) {
238 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
239
240 mText1.setText("");
241 mText1.requestFocus();
242 if (!confirmingPinCode)
243 tempText[0] = "";
244 bChange= false;
245
246 }else if(!bChange){
247 bChange=true;
248
249 }
250 return false;
251 }
252 });
253
254 mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
255
256 @Override
257 public void onFocusChange(View v, boolean hasFocus) {
258 mText2.setCursorVisible(true);
259 if (mText1.getText().toString().equals("")){
260 mText2.setSelected(false);
261 mText2.setCursorVisible(false);
262 mText1.requestFocus();
263 mText1.setSelected(true);
264 mText1.setSelection(0);
265 }
266
267 }
268 });
269
270
271 /*------------------------------------------------
272 * THIRD BOX
273 -------------------------------------------------*/
274 mText3.addTextChangedListener(new TextWatcher() {
275
276 @Override
277 public void onTextChanged(CharSequence s, int start, int before,
278 int count) {
279 }
280
281 @Override
282 public void beforeTextChanged(CharSequence s, int start, int count,
283 int after) {
284 }
285
286 @Override
287 public void afterTextChanged(Editable s) {
288 if (s.length() > 0) {
289 if (!confirmingPinCode){
290 tempText[2] = mText3.getText().toString();
291 }
292 mText4.requestFocus();
293 }
294 }
295 });
296
297 mText3.setOnKeyListener(new OnKeyListener() {
298
299 @Override
300 public boolean onKey(View v, int keyCode, KeyEvent event) {
301 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
302 mText2.requestFocus();
303 if (!confirmingPinCode)
304 tempText[1] = "";
305 mText2.setText("");
306 bChange= false;
307
308 }else if(!bChange){
309 bChange=true;
310
311 }
312 return false;
313 }
314 });
315
316 mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
317
318 @Override
319 public void onFocusChange(View v, boolean hasFocus) {
320 mText3.setCursorVisible(true);
321 if (mText1.getText().toString().equals("")){
322 mText3.setSelected(false);
323 mText3.setCursorVisible(false);
324 mText1.requestFocus();
325 mText1.setSelected(true);
326 mText1.setSelection(0);
327 }else if (mText2.getText().toString().equals("")){
328 mText3.setSelected(false);
329 mText3.setCursorVisible(false);
330 mText2.requestFocus();
331 mText2.setSelected(true);
332 mText2.setSelection(0);
333 }
334
335 }
336 });
337
338 /*------------------------------------------------
339 * FOURTH BOX
340 -------------------------------------------------*/
341 mText4.addTextChangedListener(new TextWatcher() {
342
343 @Override
344 public void onTextChanged(CharSequence s, int start, int before,
345 int count) {
346 }
347
348 @Override
349 public void beforeTextChanged(CharSequence s, int start, int count,
350 int after) {
351 }
352
353 @Override
354 public void afterTextChanged(Editable s) {
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
399
400 mText4.setOnKeyListener(new OnKeyListener() {
401
402 @Override
403 public boolean onKey(View v, int keyCode, KeyEvent event) {
404 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
405 mText3.requestFocus();
406 if (!confirmingPinCode)
407 tempText[2]="";
408 mText3.setText("");
409 bChange= false;
410
411 }else if(!bChange){
412 bChange=true;
413 }
414 return false;
415 }
416 });
417
418 mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
419
420 @Override
421 public void onFocusChange(View v, boolean hasFocus) {
422 mText4.setCursorVisible(true);
423
424 if (mText1.getText().toString().equals("")){
425 mText4.setSelected(false);
426 mText4.setCursorVisible(false);
427 mText1.requestFocus();
428 mText1.setSelected(true);
429 mText1.setSelection(0);
430 }else if (mText2.getText().toString().equals("")){
431 mText4.setSelected(false);
432 mText4.setCursorVisible(false);
433 mText2.requestFocus();
434 mText2.setSelected(true);
435 mText2.setSelection(0);
436 }else if (mText3.getText().toString().equals("")){
437 mText4.setSelected(false);
438 mText4.setCursorVisible(false);
439 mText3.requestFocus();
440 mText3.setSelected(true);
441 mText3.setSelection(0);
442 }
443
444 }
445 });
446
447
448
449 } // end setTextListener
450
451
452 protected void pinCodeChangeRequest(){
453
454 clearBoxes();
455 mPinHdr.setText(R.string.pincode_reenter_your_pincode);
456 mPinHdrExplanation.setVisibility(View.INVISIBLE);
457 confirmingPinCode =true;
458
459 }
460
461
462 protected boolean checkPincode(){
463
464
465 SharedPreferences appPrefs = PreferenceManager
466 .getDefaultSharedPreferences(getApplicationContext());
467
468 String pText1 = appPrefs.getString("PrefPinCode1", null);
469 String pText2 = appPrefs.getString("PrefPinCode2", null);
470 String pText3 = appPrefs.getString("PrefPinCode3", null);
471 String pText4 = appPrefs.getString("PrefPinCode4", null);
472
473 if ( tempText[0].equals(pText1) &&
474 tempText[1].equals(pText2) &&
475 tempText[2].equals(pText3) &&
476 tempText[3].equals(pText4) ) {
477
478 return true;
479
480
481 }else {
482 Arrays.fill(tempText, null);
483 AlertDialog aDialog = new AlertDialog.Builder(this).create();
484 CharSequence errorSeq = getString(R.string.common_error);
485 aDialog.setTitle(errorSeq);
486 CharSequence cseq = getString(R.string.pincode_wrong);
487 aDialog.setMessage(cseq);
488 CharSequence okSeq = getString(R.string.common_ok);
489 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
490
491 @Override
492 public void onClick(DialogInterface dialog, int which) {
493 return;
494 }
495
496 });
497 aDialog.show();
498 clearBoxes();
499 mPinHdr.setText(R.string.pincode_enter_pin_code);
500 mPinHdrExplanation.setVisibility(View.INVISIBLE);
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 CharSequence errorSeq = getString(R.string.common_error);
531 aDialog.setTitle(errorSeq);
532 CharSequence cseq = getString(R.string.pincode_mismatch);
533 aDialog.setMessage(cseq);
534 CharSequence okSeq = getString(R.string.common_ok);
535 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
536
537 @Override
538 public void onClick(DialogInterface dialog, int which) {
539 return;
540 }
541
542 });
543 aDialog.show();
544 mPinHdr.setText(R.string.pincode_configure_your_pin);
545 mPinHdrExplanation.setVisibility(View.VISIBLE);
546 clearBoxes();
547 }
548
549 }
550
551
552 protected void pinCodeEnd(boolean state){
553 AlertDialog aDialog = new AlertDialog.Builder(this).create();
554
555 if (state){
556 CharSequence saveSeq = getString(R.string.common_save_exit);
557 aDialog.setTitle(saveSeq);
558 CharSequence cseq = getString(R.string.pincode_stored);
559 aDialog.setMessage(cseq);
560
561 }else{
562 CharSequence saveSeq = getString(R.string.common_save_exit);
563 aDialog.setTitle(saveSeq);
564 CharSequence cseq = getString(R.string.pincode_removed);
565 aDialog.setMessage(cseq);
566
567 }
568 CharSequence okSeq = getString(R.string.common_ok);
569 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
570
571 @Override
572 public void onClick(DialogInterface dialog, int which) {
573 finish();
574 return;
575 }
576
577 });
578 aDialog.show();
579 }
580
581 protected void savePincodeAndExit(){
582 SharedPreferences.Editor appPrefs = PreferenceManager
583 .getDefaultSharedPreferences(getApplicationContext()).edit();
584
585 appPrefs.putString("PrefPinCode1", tempText[0]);
586 appPrefs.putString("PrefPinCode2",tempText[1]);
587 appPrefs.putString("PrefPinCode3", tempText[2]);
588 appPrefs.putString("PrefPinCode4", tempText[3]);
589 appPrefs.putBoolean("set_pincode",true);
590 appPrefs.commit();
591
592 pinCodeEnd(true);
593
594
595
596 }
597
598
599 protected void clearBoxes(){
600
601 mText1.setText("");
602 mText2.setText("");
603 mText3.setText("");
604 mText4.setText("");
605 mText1.requestFocus();
606 }
607
608
609 @Override
610 public boolean onKeyDown(int keyCode, KeyEvent event){
611 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
612
613 if (activity.equals("preferences")){
614 SharedPreferences.Editor appPrefsE = PreferenceManager
615
616 .getDefaultSharedPreferences(getApplicationContext()).edit();
617
618 SharedPreferences appPrefs = PreferenceManager
619 .getDefaultSharedPreferences(getApplicationContext());
620
621 boolean state = appPrefs.getBoolean("set_pincode", false);
622 appPrefsE.putBoolean("set_pincode",!state);
623 appPrefsE.commit();
624 setInitVars();
625 finish();
626 }
627 return true;
628
629 }
630
631 return super.onKeyDown(keyCode, event);
632 }
633
634
635
636
637
638 }