Temporal layout for rename
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud.ui.activity;
19
20 import com.actionbarsherlock.app.SherlockFragmentActivity;
21
22 import eu.alefzero.owncloud.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
42 public class PinCodeActivity extends SherlockFragmentActivity {
43
44
45 public final static String EXTRA_ACTIVITY = "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE = "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
47
48 Button bCancel;
49 TextView mPinHdr;
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 de 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 mText1 = (EditText) findViewById(R.id.txt1);
76 mText1.requestFocus();
77 mText2 = (EditText) findViewById(R.id.txt2);
78 mText3 = (EditText) findViewById(R.id.txt3);
79 mText4 = (EditText) findViewById(R.id.txt4);
80
81
82 SharedPreferences appPrefs = PreferenceManager
83 .getDefaultSharedPreferences(getApplicationContext());
84
85 // Not PIN Code defined yet
86 if ( appPrefs.getString("PrefPinCode1", null) == null ){
87 setChangePincodeView();
88 pinCodeChecked = true;
89 newPasswordEntered = true;
90
91 } else {
92 setInitView();
93 }
94
95
96 setTextListeners();
97
98
99 }
100
101
102 protected void setInitView(){
103 bCancel.setVisibility(View.INVISIBLE);
104 bCancel.setVisibility(View.GONE);
105 mPinHdr.setText(R.string.pincode_enter_pin_code);
106 }
107
108
109
110 protected void setChangePincodeView(){
111 mPinHdr.setText(R.string.pincode_configure_your_pin);
112 bCancel.setOnClickListener(new OnClickListener() {
113
114 @Override
115 public void onClick(View v) {
116 finish();
117 }
118 });
119
120 }
121
122 /*
123 *
124 */
125 protected void setTextListeners(){
126
127 /*------------------------------------------------
128 * FIRST BOX
129 -------------------------------------------------*/
130
131 mText1.addTextChangedListener(new TextWatcher() {
132
133 @Override
134 public void onTextChanged(CharSequence s, int start, int before,
135 int count) {
136 // TODO Auto-generated method stub
137 if (s.length() > 0) {
138 if (!confirmingPinCode){
139 tempText[0] = mText1.getText().toString();
140
141 }
142 mText2.requestFocus();
143 }
144 }
145
146 @Override
147 public void beforeTextChanged(CharSequence s, int start, int count,
148 int after) {
149 // TODO Auto-generated method stub
150
151 }
152
153 @Override
154 public void afterTextChanged(Editable s) {
155 // TODO Auto-generated method stub
156
157 }
158 });
159
160
161
162 /*------------------------------------------------
163 * SECOND BOX
164 -------------------------------------------------*/
165 mText2.addTextChangedListener(new TextWatcher() {
166
167 @Override
168 public void onTextChanged(CharSequence s, int start, int before,
169 int count) {
170 // TODO Auto-generated method stub
171 if (s.length() > 0) {
172 if (!confirmingPinCode){
173 tempText[1] = mText2.getText().toString();
174 }
175 mText3.requestFocus();
176 }
177 }
178
179 @Override
180 public void beforeTextChanged(CharSequence s, int start, int count,
181 int after) {
182 // TODO Auto-generated method stub
183
184 }
185
186 @Override
187 public void afterTextChanged(Editable s) {
188 // TODO Auto-generated method stub
189
190 }
191 });
192
193 mText2.setOnKeyListener(new OnKeyListener() {
194
195 @Override
196 public boolean onKey(View v, int keyCode, KeyEvent event) {
197 // TODO Auto-generated method stub
198
199 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
200
201 mText1.setText("");
202 mText1.requestFocus();
203 tempText[0] = "";
204 bChange= false;
205
206 }else if(!bChange){
207 bChange=true;
208
209 }
210 return false;
211 }
212 });
213
214 mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
215
216 @Override
217 public void onFocusChange(View v, boolean hasFocus) {
218 // TODO Auto-generated method stub
219 if (mText1.getText().toString().equals("")){
220 mText1.requestFocus();
221 }
222
223 }
224 });
225
226
227 /*------------------------------------------------
228 * THIRD BOX
229 -------------------------------------------------*/
230 mText3.addTextChangedListener(new TextWatcher() {
231
232 @Override
233 public void onTextChanged(CharSequence s, int start, int before,
234 int count) {
235 // TODO Auto-generated method stub
236 if (s.length() > 0) {
237 if (!confirmingPinCode){
238 tempText[2] = mText3.getText().toString();
239 }
240 mText4.requestFocus();
241 }
242 }
243
244 @Override
245 public void beforeTextChanged(CharSequence s, int start, int count,
246 int after) {
247 // TODO Auto-generated method stub
248
249 }
250
251 @Override
252 public void afterTextChanged(Editable s) {
253 // TODO Auto-generated method stub
254
255 }
256 });
257
258 mText3.setOnKeyListener(new OnKeyListener() {
259
260 @Override
261 public boolean onKey(View v, int keyCode, KeyEvent event) {
262 // TODO Auto-generated method stub
263
264 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
265 mText2.requestFocus();
266 tempText[1] = "";
267 mText2.setText("");
268 bChange= false;
269
270 }else if(!bChange){
271 bChange=true;
272
273 }
274 return false;
275 }
276 });
277
278 mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
279
280 @Override
281 public void onFocusChange(View v, boolean hasFocus) {
282 // TODO Auto-generated method stub
283 if (mText1.getText().toString().equals("")){
284 mText1.requestFocus();
285 }else if (mText2.getText().toString().equals("")){
286 mText2.requestFocus();
287 }
288
289 }
290 });
291
292 /*------------------------------------------------
293 * FOURTH BOX
294 -------------------------------------------------*/
295 mText4.addTextChangedListener(new TextWatcher() {
296
297 @Override
298 public void onTextChanged(CharSequence s, int start, int before,
299 int count) {
300
301 if (s.length() > 0) {
302
303 if (!confirmingPinCode){
304 tempText[3] = mText4.getText().toString();
305 }
306 mText1.requestFocus();
307
308 if (!pinCodeChecked){
309 pinCodeChecked = checkPincode();
310 }
311
312 if (pinCodeChecked && activity.equals("FileDisplayActivity")){
313 finish();
314 } else if (pinCodeChecked){
315
316 Intent intent = getIntent();
317 String newState = intent.getStringExtra(EXTRA_NEW_STATE);
318
319 if (newState.equals("false")){
320 SharedPreferences.Editor appPrefs = PreferenceManager
321 .getDefaultSharedPreferences(getApplicationContext()).edit();
322 appPrefs.putBoolean("set_pincode",false);
323 appPrefs.commit();
324 // TODO Alert Message que salte y vuelva a la pantalla anterior
325
326 finish();
327
328 }else{
329
330 if (!confirmingPinCode && !newPasswordEntered){
331 pinCodeChangeRequest();
332 } else if (newPasswordEntered && !confirmingPinCode){
333 mPinHdr.setText(R.string.pincode_confirm_your_pincode);
334 confirmingPinCode = true;
335 clearBoxes();
336 } else {
337 confirmPincode();
338 }
339 }
340
341
342 }
343
344 }
345 }
346
347 @Override
348 public void beforeTextChanged(CharSequence s, int start, int count,
349 int after) {
350 // TODO Auto-generated method stub
351
352 }
353
354 @Override
355 public void afterTextChanged(Editable s) {
356 // TODO Auto-generated method stub
357
358 }
359 });
360
361
362
363 mText4.setOnKeyListener(new OnKeyListener() {
364
365 @Override
366 public boolean onKey(View v, int keyCode, KeyEvent event) {
367 // TODO Auto-generated method stub
368
369 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
370 mText3.requestFocus();
371 tempText[2]="";
372 mText3.setText("");
373 bChange= false;
374
375 }else if(!bChange){
376 bChange=true;
377 }
378 return false;
379 }
380 });
381
382 mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
383
384 @Override
385 public void onFocusChange(View v, boolean hasFocus) {
386 // TODO Auto-generated method stub
387 if (mText1.getText().toString().equals("")){
388 mText1.requestFocus();
389 }else if (mText2.getText().toString().equals("")){
390 mText2.requestFocus();
391 }else if (mText3.getText().toString().equals("")){
392 mText3.requestFocus();
393 }
394
395 }
396 });
397
398
399
400 } // end setTextListener
401
402
403 protected void pinCodeChangeRequest(){
404
405 AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);
406 aBuilder.setMessage("Do yo want to set a new PIN Code")
407 .setCancelable(false)
408 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
409
410 @Override
411 public void onClick(DialogInterface dialog, int which) {
412 // TODO Auto-generated method stub
413 setChangePincodeView();
414 mPinHdr.setText(R.string.pincode_enter_new_pin_code);
415 clearBoxes();
416 newPasswordEntered = true;
417 }
418 })
419 .setNegativeButton("No", new DialogInterface.OnClickListener() {
420
421 @Override
422 public void onClick(DialogInterface dialog, int which) {
423 // TODO Auto-generated method stub
424 SharedPreferences.Editor appPrefs = PreferenceManager
425 .getDefaultSharedPreferences(getApplicationContext()).edit();
426 appPrefs.putBoolean("set_pincode",false);
427 appPrefs.commit();
428 finish();
429 }
430 });
431
432 AlertDialog alert =aBuilder.create();
433
434 alert.show();
435
436
437 }
438
439
440 protected boolean checkPincode(){
441
442
443 SharedPreferences appPrefs = PreferenceManager
444 .getDefaultSharedPreferences(getApplicationContext());
445
446 String pText1 = appPrefs.getString("PrefPinCode1", null);
447 String pText2 = appPrefs.getString("PrefPinCode2", null);
448 String pText3 = appPrefs.getString("PrefPinCode3", null);
449 String pText4 = appPrefs.getString("PrefPinCode4", null);
450
451 if ( tempText[0].equals(pText1) &&
452 tempText[1].equals(pText2) &&
453 tempText[2].equals(pText3) &&
454 tempText[3].equals(pText4) ) {
455
456 return true;
457
458
459 }else {
460 AlertDialog aDialog = new AlertDialog.Builder(this).create();
461 aDialog.setTitle("ERROR");
462 aDialog.setMessage("Wrong PIN");
463 aDialog.setButton("OK", new DialogInterface.OnClickListener(){
464
465 @Override
466 public void onClick(DialogInterface dialog, int which) {
467 // TODO Auto-generated method stub("");
468 return;
469 }
470
471 });
472 aDialog.show();
473 clearBoxes();
474 mPinHdr.setText(R.string.pincode_configure_your_pin);
475 newPasswordEntered = true;
476 confirmingPinCode = false;
477
478 }
479
480
481 return false;
482 }
483
484 protected void confirmPincode(){
485
486 confirmingPinCode = false;
487
488 String rText1 = mText1.getText().toString();
489 String rText2 = mText2.getText().toString();
490 String rText3 = mText3.getText().toString();
491 String rText4 = mText4.getText().toString();
492
493 if ( tempText[0].equals(rText1) &&
494 tempText[1].equals(rText2) &&
495 tempText[2].equals(rText3) &&
496 tempText[3].equals(rText4) ) {
497
498 savePincodeAndExit();
499
500 } else {
501
502 AlertDialog aDialog = new AlertDialog.Builder(this).create();
503 aDialog.setTitle("ERROR");
504 aDialog.setMessage("PIN Code Mismatch");
505 aDialog.setButton("OK", new DialogInterface.OnClickListener(){
506
507 @Override
508 public void onClick(DialogInterface dialog, int which) {
509 // TODO Auto-generated method stub("");
510 return;
511 }
512
513 });
514 aDialog.show();
515 mPinHdr.setText(R.string.pincode_configure_your_pin);
516 clearBoxes();
517 }
518
519 }
520
521 protected void savePincodeAndExit(){
522 SharedPreferences.Editor appPrefs = PreferenceManager
523 .getDefaultSharedPreferences(getApplicationContext()).edit();
524
525 appPrefs.putString("PrefPinCode1", tempText[0]);
526 appPrefs.putString("PrefPinCode2",tempText[1]);
527 appPrefs.putString("PrefPinCode3", tempText[2]);
528 appPrefs.putString("PrefPinCode4", tempText[3]);
529 appPrefs.putBoolean("set_pincode",true);
530 appPrefs.commit();
531
532 finish();
533 }
534
535
536 protected void clearBoxes(){
537
538 mText1.setText("");
539 mText2.setText("");
540 mText3.setText("");
541 mText4.setText("");
542 mText1.requestFocus();
543 }
544
545
546 }