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