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