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