OC-2332: rewrite getAvailableRemotePath from UpdateFileOperation, using ExistenceCehe...
[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
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 public class PinCodeActivity extends SherlockFragmentActivity {
42
43
44 public final static String EXTRA_ACTIVITY = "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
45 public final static String EXTRA_NEW_STATE = "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
46
47 Button bCancel;
48 TextView mPinHdr;
49 TextView mPinHdrExplanation;
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 mPinHdrExplanation = (TextView) findViewById(R.id.pinHdrExpl);
76 mText1 = (EditText) findViewById(R.id.txt1);
77 mText1.requestFocus();
78 getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
79 mText2 = (EditText) findViewById(R.id.txt2);
80 mText3 = (EditText) findViewById(R.id.txt3);
81 mText4 = (EditText) findViewById(R.id.txt4);
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 setChangePincodeView(true);
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 mPinHdrExplanation.setVisibility(View.VISIBLE);
102 pinCodeChecked = true ; // No need to check it
103 setChangePincodeView(true);
104 }else{
105 // PIN active
106 bCancel.setVisibility(View.INVISIBLE);
107 bCancel.setVisibility(View.GONE);
108 mPinHdr.setText(R.string.pincode_enter_pin_code);
109 mPinHdrExplanation.setVisibility(View.INVISIBLE);
110 setChangePincodeView(false);
111 }
112
113 }else {
114 // pincode removal
115 mPinHdr.setText(R.string.pincode_remove_your_pincode);
116 mPinHdrExplanation.setVisibility(View.INVISIBLE);
117 pinCodeChecked = false;
118 setChangePincodeView(true);
119 }
120
121 }
122 setTextListeners();
123
124
125 }
126
127
128
129 protected void setInitVars(){
130 confirmingPinCode = false;
131 pinCodeChecked = false;
132 newPasswordEntered = false;
133
134 }
135
136 protected void setInitView(){
137 bCancel.setVisibility(View.INVISIBLE);
138 bCancel.setVisibility(View.GONE);
139 mPinHdr.setText(R.string.pincode_enter_pin_code);
140 mPinHdrExplanation.setVisibility(View.INVISIBLE);
141 }
142
143
144 protected void setChangePincodeView(boolean state){
145
146 if(state){
147 bCancel.setVisibility(View.VISIBLE);
148 bCancel.setOnClickListener(new OnClickListener() {
149 @Override
150 public void onClick(View v) {
151
152 SharedPreferences.Editor appPrefsE = PreferenceManager
153 .getDefaultSharedPreferences(getApplicationContext()).edit();
154
155 SharedPreferences appPrefs = PreferenceManager
156 .getDefaultSharedPreferences(getApplicationContext());
157
158 boolean state = appPrefs.getBoolean("set_pincode", false);
159 appPrefsE.putBoolean("set_pincode",!state);
160 appPrefsE.commit();
161 setInitVars();
162 finish();
163 }
164 });
165 }
166
167 }
168
169
170
171 /*
172 *
173 */
174 protected void setTextListeners(){
175
176 /*------------------------------------------------
177 * FIRST BOX
178 -------------------------------------------------*/
179
180 mText1.addTextChangedListener(new TextWatcher() {
181
182 @Override
183 public void onTextChanged(CharSequence s, int start, int before,
184 int count) {
185 }
186
187 @Override
188 public void beforeTextChanged(CharSequence s, int start, int count,
189 int after) {
190 }
191
192 @Override
193 public void afterTextChanged(Editable s) {
194 if (s.length() > 0) {
195 if (!confirmingPinCode){
196 tempText[0] = mText1.getText().toString();
197
198 }
199 mText2.requestFocus();
200 }
201 }
202 });
203
204
205
206 /*------------------------------------------------
207 * SECOND BOX
208 -------------------------------------------------*/
209 mText2.addTextChangedListener(new TextWatcher() {
210
211 @Override
212 public void onTextChanged(CharSequence s, int start, int before,
213 int count) {
214 }
215
216 @Override
217 public void beforeTextChanged(CharSequence s, int start, int count,
218 int after) {
219 }
220
221 @Override
222 public void afterTextChanged(Editable s) {
223 if (s.length() > 0) {
224 if (!confirmingPinCode){
225 tempText[1] = mText2.getText().toString();
226 }
227
228 mText3.requestFocus();
229 }
230 }
231 });
232
233 mText2.setOnKeyListener(new OnKeyListener() {
234
235 @Override
236 public boolean onKey(View v, int keyCode, KeyEvent event) {
237 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
238
239 mText1.setText("");
240 mText1.requestFocus();
241 if (!confirmingPinCode)
242 tempText[0] = "";
243 bChange= false;
244
245 }else if(!bChange){
246 bChange=true;
247
248 }
249 return false;
250 }
251 });
252
253 mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
254
255 @Override
256 public void onFocusChange(View v, boolean hasFocus) {
257 mText2.setCursorVisible(true);
258 if (mText1.getText().toString().equals("")){
259 mText2.setSelected(false);
260 mText2.setCursorVisible(false);
261 mText1.requestFocus();
262 mText1.setSelected(true);
263 mText1.setSelection(0);
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 }
279
280 @Override
281 public void beforeTextChanged(CharSequence s, int start, int count,
282 int after) {
283 }
284
285 @Override
286 public void afterTextChanged(Editable s) {
287 if (s.length() > 0) {
288 if (!confirmingPinCode){
289 tempText[2] = mText3.getText().toString();
290 }
291 mText4.requestFocus();
292 }
293 }
294 });
295
296 mText3.setOnKeyListener(new OnKeyListener() {
297
298 @Override
299 public boolean onKey(View v, int keyCode, KeyEvent event) {
300 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
301 mText2.requestFocus();
302 if (!confirmingPinCode)
303 tempText[1] = "";
304 mText2.setText("");
305 bChange= false;
306
307 }else if(!bChange){
308 bChange=true;
309
310 }
311 return false;
312 }
313 });
314
315 mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
316
317 @Override
318 public void onFocusChange(View v, boolean hasFocus) {
319 mText3.setCursorVisible(true);
320 if (mText1.getText().toString().equals("")){
321 mText3.setSelected(false);
322 mText3.setCursorVisible(false);
323 mText1.requestFocus();
324 mText1.setSelected(true);
325 mText1.setSelection(0);
326 }else if (mText2.getText().toString().equals("")){
327 mText3.setSelected(false);
328 mText3.setCursorVisible(false);
329 mText2.requestFocus();
330 mText2.setSelected(true);
331 mText2.setSelection(0);
332 }
333
334 }
335 });
336
337 /*------------------------------------------------
338 * FOURTH BOX
339 -------------------------------------------------*/
340 mText4.addTextChangedListener(new TextWatcher() {
341
342 @Override
343 public void onTextChanged(CharSequence s, int start, int before,
344 int count) {
345 }
346
347 @Override
348 public void beforeTextChanged(CharSequence s, int start, int count,
349 int after) {
350 }
351
352 @Override
353 public void afterTextChanged(Editable s) {
354 if (s.length() > 0) {
355
356 if (!confirmingPinCode){
357 tempText[3] = mText4.getText().toString();
358 }
359 mText1.requestFocus();
360
361 if (!pinCodeChecked){
362 pinCodeChecked = checkPincode();
363 }
364
365 if (pinCodeChecked && activity.equals("FileDisplayActivity")){
366 finish();
367 } else if (pinCodeChecked){
368
369 Intent intent = getIntent();
370 String newState = intent.getStringExtra(EXTRA_NEW_STATE);
371
372 if (newState.equals("false")){
373 SharedPreferences.Editor appPrefs = PreferenceManager
374 .getDefaultSharedPreferences(getApplicationContext()).edit();
375 appPrefs.putBoolean("set_pincode",false);
376 appPrefs.commit();
377
378 setInitVars();
379 pinCodeEnd(false);
380
381 }else{
382
383 if (!confirmingPinCode){
384 pinCodeChangeRequest();
385
386 } else {
387 confirmPincode();
388 }
389 }
390
391
392 }
393 }
394 }
395 });
396
397
398
399 mText4.setOnKeyListener(new OnKeyListener() {
400
401 @Override
402 public boolean onKey(View v, int keyCode, KeyEvent event) {
403 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
404 mText3.requestFocus();
405 if (!confirmingPinCode)
406 tempText[2]="";
407 mText3.setText("");
408 bChange= false;
409
410 }else if(!bChange){
411 bChange=true;
412 }
413 return false;
414 }
415 });
416
417 mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
418
419 @Override
420 public void onFocusChange(View v, boolean hasFocus) {
421 mText4.setCursorVisible(true);
422
423 if (mText1.getText().toString().equals("")){
424 mText4.setSelected(false);
425 mText4.setCursorVisible(false);
426 mText1.requestFocus();
427 mText1.setSelected(true);
428 mText1.setSelection(0);
429 }else if (mText2.getText().toString().equals("")){
430 mText4.setSelected(false);
431 mText4.setCursorVisible(false);
432 mText2.requestFocus();
433 mText2.setSelected(true);
434 mText2.setSelection(0);
435 }else if (mText3.getText().toString().equals("")){
436 mText4.setSelected(false);
437 mText4.setCursorVisible(false);
438 mText3.requestFocus();
439 mText3.setSelected(true);
440 mText3.setSelection(0);
441 }
442
443 }
444 });
445
446
447
448 } // end setTextListener
449
450
451 protected void pinCodeChangeRequest(){
452
453 clearBoxes();
454 mPinHdr.setText(R.string.pincode_reenter_your_pincode);
455 mPinHdrExplanation.setVisibility(View.INVISIBLE);
456 confirmingPinCode =true;
457
458 }
459
460
461 protected boolean checkPincode(){
462
463
464 SharedPreferences appPrefs = PreferenceManager
465 .getDefaultSharedPreferences(getApplicationContext());
466
467 String pText1 = appPrefs.getString("PrefPinCode1", null);
468 String pText2 = appPrefs.getString("PrefPinCode2", null);
469 String pText3 = appPrefs.getString("PrefPinCode3", null);
470 String pText4 = appPrefs.getString("PrefPinCode4", null);
471
472 if ( tempText[0].equals(pText1) &&
473 tempText[1].equals(pText2) &&
474 tempText[2].equals(pText3) &&
475 tempText[3].equals(pText4) ) {
476
477 return true;
478
479
480 }else {
481 Arrays.fill(tempText, null);
482 AlertDialog aDialog = new AlertDialog.Builder(this).create();
483 CharSequence errorSeq = getString(R.string.common_error);
484 aDialog.setTitle(errorSeq);
485 CharSequence cseq = getString(R.string.pincode_wrong);
486 aDialog.setMessage(cseq);
487 CharSequence okSeq = getString(R.string.common_ok);
488 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
489
490 @Override
491 public void onClick(DialogInterface dialog, int which) {
492 return;
493 }
494
495 });
496 aDialog.show();
497 clearBoxes();
498 mPinHdr.setText(R.string.pincode_enter_pin_code);
499 mPinHdrExplanation.setVisibility(View.INVISIBLE);
500 newPasswordEntered = true;
501 confirmingPinCode = false;
502
503 }
504
505
506 return false;
507 }
508
509 protected void confirmPincode(){
510
511 confirmingPinCode = false;
512
513 String rText1 = mText1.getText().toString();
514 String rText2 = mText2.getText().toString();
515 String rText3 = mText3.getText().toString();
516 String rText4 = mText4.getText().toString();
517
518 if ( tempText[0].equals(rText1) &&
519 tempText[1].equals(rText2) &&
520 tempText[2].equals(rText3) &&
521 tempText[3].equals(rText4) ) {
522
523 savePincodeAndExit();
524
525 } else {
526
527 Arrays.fill(tempText, null);
528 AlertDialog aDialog = new AlertDialog.Builder(this).create();
529 CharSequence errorSeq = getString(R.string.common_error);
530 aDialog.setTitle(errorSeq);
531 CharSequence cseq = getString(R.string.pincode_mismatch);
532 aDialog.setMessage(cseq);
533 CharSequence okSeq = getString(R.string.common_ok);
534 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
535
536 @Override
537 public void onClick(DialogInterface dialog, int which) {
538 return;
539 }
540
541 });
542 aDialog.show();
543 mPinHdr.setText(R.string.pincode_configure_your_pin);
544 mPinHdrExplanation.setVisibility(View.VISIBLE);
545 clearBoxes();
546 }
547
548 }
549
550
551 protected void pinCodeEnd(boolean state){
552 AlertDialog aDialog = new AlertDialog.Builder(this).create();
553
554 if (state){
555 CharSequence saveSeq = getString(R.string.common_save_exit);
556 aDialog.setTitle(saveSeq);
557 CharSequence cseq = getString(R.string.pincode_stored);
558 aDialog.setMessage(cseq);
559
560 }else{
561 CharSequence saveSeq = getString(R.string.common_save_exit);
562 aDialog.setTitle(saveSeq);
563 CharSequence cseq = getString(R.string.pincode_removed);
564 aDialog.setMessage(cseq);
565
566 }
567 CharSequence okSeq = getString(R.string.common_ok);
568 aDialog.setButton(okSeq, new DialogInterface.OnClickListener(){
569
570 @Override
571 public void onClick(DialogInterface dialog, int which) {
572 finish();
573 return;
574 }
575
576 });
577 aDialog.show();
578 }
579
580 protected void savePincodeAndExit(){
581 SharedPreferences.Editor appPrefs = PreferenceManager
582 .getDefaultSharedPreferences(getApplicationContext()).edit();
583
584 appPrefs.putString("PrefPinCode1", tempText[0]);
585 appPrefs.putString("PrefPinCode2",tempText[1]);
586 appPrefs.putString("PrefPinCode3", tempText[2]);
587 appPrefs.putString("PrefPinCode4", tempText[3]);
588 appPrefs.putBoolean("set_pincode",true);
589 appPrefs.commit();
590
591 pinCodeEnd(true);
592
593
594
595 }
596
597
598 protected void clearBoxes(){
599
600 mText1.setText("");
601 mText2.setText("");
602 mText3.setText("");
603 mText4.setText("");
604 mText1.requestFocus();
605 }
606
607
608 @Override
609 public boolean onKeyDown(int keyCode, KeyEvent event){
610 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
611
612 if (activity.equals("preferences")){
613 SharedPreferences.Editor appPrefsE = PreferenceManager
614
615 .getDefaultSharedPreferences(getApplicationContext()).edit();
616
617 SharedPreferences appPrefs = PreferenceManager
618 .getDefaultSharedPreferences(getApplicationContext());
619
620 boolean state = appPrefs.getBoolean("set_pincode", false);
621 appPrefsE.putBoolean("set_pincode",!state);
622 appPrefsE.commit();
623 setInitVars();
624 finish();
625 }
626 return true;
627
628 }
629
630 return super.onKeyDown(keyCode, event);
631 }
632
633
634
635
636
637 }