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