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
{
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";
55 String
[] tempText
={"","","",""};
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
66 protected void onCreate(Bundle savedInstanceState
) {
67 super.onCreate(savedInstanceState
);
68 setContentView(R
.layout
.pincodelock
);
70 Intent intent
= getIntent();
71 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
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
);
82 SharedPreferences appPrefs
= PreferenceManager
83 .getDefaultSharedPreferences(getApplicationContext());
85 // Not PIN Code defined yet
86 if ( appPrefs
.getString("PrefPinCode1", null
) == null
){
87 setChangePincodeView();
88 pinCodeChecked
= true
;
89 newPasswordEntered
= true
;
102 protected void setInitView(){
103 bCancel
.setVisibility(View
.INVISIBLE
);
104 bCancel
.setVisibility(View
.GONE
);
105 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
110 protected void setChangePincodeView(){
111 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
112 bCancel
.setOnClickListener(new OnClickListener() {
115 public void onClick(View v
) {
125 protected void setTextListeners(){
127 /*------------------------------------------------
129 -------------------------------------------------*/
131 mText1
.addTextChangedListener(new TextWatcher() {
134 public void onTextChanged(CharSequence s
, int start
, int before
,
136 // TODO Auto-generated method stub
137 if (s
.length() > 0) {
138 if (!confirmingPinCode
){
139 tempText
[0] = mText1
.getText().toString();
142 mText2
.requestFocus();
147 public void beforeTextChanged(CharSequence s
, int start
, int count
,
149 // TODO Auto-generated method stub
154 public void afterTextChanged(Editable s
) {
155 // TODO Auto-generated method stub
162 /*------------------------------------------------
164 -------------------------------------------------*/
165 mText2
.addTextChangedListener(new TextWatcher() {
168 public void onTextChanged(CharSequence s
, int start
, int before
,
170 // TODO Auto-generated method stub
171 if (s
.length() > 0) {
172 if (!confirmingPinCode
){
173 tempText
[1] = mText2
.getText().toString();
175 mText3
.requestFocus();
180 public void beforeTextChanged(CharSequence s
, int start
, int count
,
182 // TODO Auto-generated method stub
187 public void afterTextChanged(Editable s
) {
188 // TODO Auto-generated method stub
193 mText2
.setOnKeyListener(new OnKeyListener() {
196 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
197 // TODO Auto-generated method stub
199 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
202 mText1
.requestFocus();
214 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
217 public void onFocusChange(View v
, boolean hasFocus
) {
218 // TODO Auto-generated method stub
219 if (mText1
.getText().toString().equals("")){
220 mText1
.requestFocus();
227 /*------------------------------------------------
229 -------------------------------------------------*/
230 mText3
.addTextChangedListener(new TextWatcher() {
233 public void onTextChanged(CharSequence s
, int start
, int before
,
235 // TODO Auto-generated method stub
236 if (s
.length() > 0) {
237 if (!confirmingPinCode
){
238 tempText
[2] = mText3
.getText().toString();
240 mText4
.requestFocus();
245 public void beforeTextChanged(CharSequence s
, int start
, int count
,
247 // TODO Auto-generated method stub
252 public void afterTextChanged(Editable s
) {
253 // TODO Auto-generated method stub
258 mText3
.setOnKeyListener(new OnKeyListener() {
261 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
262 // TODO Auto-generated method stub
264 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
265 mText2
.requestFocus();
278 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
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();
292 /*------------------------------------------------
294 -------------------------------------------------*/
295 mText4
.addTextChangedListener(new TextWatcher() {
298 public void onTextChanged(CharSequence s
, int start
, int before
,
301 if (s
.length() > 0) {
303 if (!confirmingPinCode
){
304 tempText
[3] = mText4
.getText().toString();
306 mText1
.requestFocus();
308 if (!pinCodeChecked
){
309 pinCodeChecked
= checkPincode();
312 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
314 } else if (pinCodeChecked
){
316 Intent intent
= getIntent();
317 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
319 if (newState
.equals("false")){
320 SharedPreferences
.Editor appPrefs
= PreferenceManager
321 .getDefaultSharedPreferences(getApplicationContext()).edit();
322 appPrefs
.putBoolean("set_pincode",false
);
324 // TODO Alert Message que salte y vuelva a la pantalla anterior
330 if (!confirmingPinCode
&& !newPasswordEntered
){
331 pinCodeChangeRequest();
332 } else if (newPasswordEntered
&& !confirmingPinCode
){
333 mPinHdr
.setText(R
.string
.pincode_confirm_your_pincode
);
334 confirmingPinCode
= true
;
348 public void beforeTextChanged(CharSequence s
, int start
, int count
,
350 // TODO Auto-generated method stub
355 public void afterTextChanged(Editable s
) {
356 // TODO Auto-generated method stub
363 mText4
.setOnKeyListener(new OnKeyListener() {
366 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
367 // TODO Auto-generated method stub
369 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
370 mText3
.requestFocus();
382 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
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();
400 } // end setTextListener
403 protected void pinCodeChangeRequest(){
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() {
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
);
416 newPasswordEntered
= true
;
419 .setNegativeButton("No", new DialogInterface
.OnClickListener() {
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
);
432 AlertDialog alert
=aBuilder
.create();
440 protected boolean checkPincode(){
443 SharedPreferences appPrefs
= PreferenceManager
444 .getDefaultSharedPreferences(getApplicationContext());
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
);
451 if ( tempText
[0].equals(pText1
) &&
452 tempText
[1].equals(pText2
) &&
453 tempText
[2].equals(pText3
) &&
454 tempText
[3].equals(pText4
) ) {
460 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
461 aDialog
.setTitle("ERROR");
462 aDialog
.setMessage("Wrong PIN");
463 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
466 public void onClick(DialogInterface dialog
, int which
) {
467 // TODO Auto-generated method stub("");
474 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
475 newPasswordEntered
= true
;
476 confirmingPinCode
= false
;
484 protected void confirmPincode(){
486 confirmingPinCode
= false
;
488 String rText1
= mText1
.getText().toString();
489 String rText2
= mText2
.getText().toString();
490 String rText3
= mText3
.getText().toString();
491 String rText4
= mText4
.getText().toString();
493 if ( tempText
[0].equals(rText1
) &&
494 tempText
[1].equals(rText2
) &&
495 tempText
[2].equals(rText3
) &&
496 tempText
[3].equals(rText4
) ) {
498 savePincodeAndExit();
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(){
508 public void onClick(DialogInterface dialog
, int which
) {
509 // TODO Auto-generated method stub("");
515 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
521 protected void savePincodeAndExit(){
522 SharedPreferences
.Editor appPrefs
= PreferenceManager
523 .getDefaultSharedPreferences(getApplicationContext()).edit();
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
);
536 protected void clearBoxes(){
542 mText1
.requestFocus();