1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.owncloud
.ui
.activity
;
20 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
22 import eu
.alefzero
.owncloud
.R
;
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
;
42 public class PinCodeActivity
extends SherlockFragmentActivity
{
53 String
[] tempText
={"","","",""};
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
64 protected void onCreate(Bundle savedInstanceState
) {
65 super.onCreate(savedInstanceState
);
66 setContentView(R
.layout
.pincodelock
);
68 Intent intent
= getIntent();
69 activity
= intent
.getStringExtra("activity");
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
);
80 SharedPreferences appPrefs
= PreferenceManager
81 .getDefaultSharedPreferences(getApplicationContext());
83 // Not PIN Code defined yet
84 if ( appPrefs
.getString("PrefPinCode1", null
) == null
){
85 setChangePincodeView();
86 pinCodeChecked
= true
;
87 newPasswordEntered
= true
;
100 protected void setInitView(){
101 bCancel
.setVisibility(View
.INVISIBLE
);
102 bCancel
.setVisibility(View
.GONE
);
103 mPinHdr
.setText("Please, Insert your PIN");
108 protected void setChangePincodeView(){
109 mPinHdr
.setText("Configure your PIN");
110 bCancel
.setOnClickListener(new OnClickListener() {
113 public void onClick(View v
) {
114 // TODO Auto-generated method stub
124 protected void setTextListeners(){
126 /*------------------------------------------------
128 -------------------------------------------------*/
130 mText1
.addTextChangedListener(new TextWatcher() {
133 public void onTextChanged(CharSequence s
, int start
, int before
,
135 // TODO Auto-generated method stub
136 if (s
.length() > 0) {
137 if (!confirmingPinCode
){
138 tempText
[0] = mText1
.getText().toString();
141 mText2
.requestFocus();
146 public void beforeTextChanged(CharSequence s
, int start
, int count
,
148 // TODO Auto-generated method stub
153 public void afterTextChanged(Editable s
) {
154 // TODO Auto-generated method stub
161 /*------------------------------------------------
163 -------------------------------------------------*/
164 mText2
.addTextChangedListener(new TextWatcher() {
167 public void onTextChanged(CharSequence s
, int start
, int before
,
169 // TODO Auto-generated method stub
170 if (s
.length() > 0) {
171 if (!confirmingPinCode
){
172 tempText
[1] = mText2
.getText().toString();
174 mText3
.requestFocus();
179 public void beforeTextChanged(CharSequence s
, int start
, int count
,
181 // TODO Auto-generated method stub
186 public void afterTextChanged(Editable s
) {
187 // TODO Auto-generated method stub
192 mText2
.setOnKeyListener(new OnKeyListener() {
195 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
196 // TODO Auto-generated method stub
198 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
201 mText1
.requestFocus();
213 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
216 public void onFocusChange(View v
, boolean hasFocus
) {
217 // TODO Auto-generated method stub
218 if (mText1
.getText().toString().equals("")){
219 mText1
.requestFocus();
226 /*------------------------------------------------
228 -------------------------------------------------*/
229 mText3
.addTextChangedListener(new TextWatcher() {
232 public void onTextChanged(CharSequence s
, int start
, int before
,
234 // TODO Auto-generated method stub
235 if (s
.length() > 0) {
236 if (!confirmingPinCode
){
237 tempText
[2] = mText3
.getText().toString();
239 mText4
.requestFocus();
244 public void beforeTextChanged(CharSequence s
, int start
, int count
,
246 // TODO Auto-generated method stub
251 public void afterTextChanged(Editable s
) {
252 // TODO Auto-generated method stub
257 mText3
.setOnKeyListener(new OnKeyListener() {
260 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
261 // TODO Auto-generated method stub
263 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
264 mText2
.requestFocus();
277 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
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();
291 /*------------------------------------------------
293 -------------------------------------------------*/
294 mText4
.addTextChangedListener(new TextWatcher() {
297 public void onTextChanged(CharSequence s
, int start
, int before
,
299 // TODO Auto-generated method stub
300 if (s
.length() > 0) {
302 if (!confirmingPinCode
){
303 tempText
[3] = mText4
.getText().toString();
305 mText1
.requestFocus();
307 if (!pinCodeChecked
){
308 pinCodeChecked
= checkPincode();
311 if (pinCodeChecked
&& activity
.equals("splash")){
313 } else if (pinCodeChecked
){
315 Intent intent
= getIntent();
316 String newState
= intent
.getStringExtra("pinNewState");
318 if (newState
.equals("false")){
319 SharedPreferences
.Editor appPrefs
= PreferenceManager
320 .getDefaultSharedPreferences(getApplicationContext()).edit();
321 appPrefs
.putBoolean("set_pincode",false
);
324 // TODO Alert Message que salte y vuelva a la pantalla anterior
331 if (!confirmingPinCode
&& !newPasswordEntered
){
332 pinCodeChangeRequest();
333 }else if (newPasswordEntered
&& !confirmingPinCode
){
334 mPinHdr
.setText("Confirm your PINCode, please");
335 confirmingPinCode
= true
;
349 public void beforeTextChanged(CharSequence s
, int start
, int count
,
351 // TODO Auto-generated method stub
356 public void afterTextChanged(Editable s
) {
357 // TODO Auto-generated method stub
364 mText4
.setOnKeyListener(new OnKeyListener() {
367 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
368 // TODO Auto-generated method stub
370 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
371 mText3
.requestFocus();
383 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
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();
401 } // end setTextListener
404 protected void pinCodeChangeRequest(){
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() {
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");
417 newPasswordEntered
= true
;
420 .setNegativeButton("No", new DialogInterface
.OnClickListener() {
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
);
433 AlertDialog alert
=aBuilder
.create();
441 protected boolean checkPincode(){
444 SharedPreferences appPrefs
= PreferenceManager
445 .getDefaultSharedPreferences(getApplicationContext());
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
);
452 if ( tempText
[0].equals(pText1
) &&
453 tempText
[1].equals(pText2
) &&
454 tempText
[2].equals(pText3
) &&
455 tempText
[3].equals(pText4
) ) {
461 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
462 aDialog
.setTitle("ERROR");
463 aDialog
.setMessage("Wrong PIN");
464 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
467 public void onClick(DialogInterface dialog
, int which
) {
468 // TODO Auto-generated method stub("");
475 mPinHdr
.setText("Configure your PIN");
476 newPasswordEntered
= true
;
477 confirmingPinCode
= false
;
485 protected void confirmPincode(){
487 confirmingPinCode
= false
;
489 String rText1
= mText1
.getText().toString();
490 String rText2
= mText2
.getText().toString();
491 String rText3
= mText3
.getText().toString();
492 String rText4
= mText4
.getText().toString();
494 if ( tempText
[0].equals(rText1
) &&
495 tempText
[1].equals(rText2
) &&
496 tempText
[2].equals(rText3
) &&
497 tempText
[3].equals(rText4
) ) {
499 savePincodeAndExit();
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(){
509 public void onClick(DialogInterface dialog
, int which
) {
510 // TODO Auto-generated method stub("");
516 mPinHdr
.setText("Configure your PIN");
522 protected void savePincodeAndExit(){
523 SharedPreferences
.Editor appPrefs
= PreferenceManager
524 .getDefaultSharedPreferences(getApplicationContext()).edit();
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
);
537 protected void clearBoxes(){
543 mText1
.requestFocus();