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