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