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