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