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