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