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 java
.util
.Arrays
;
22 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
24 import eu
.alefzero
.owncloud
.R
;
26 import android
.app
.AlertDialog
;
27 import android
.content
.DialogInterface
;
28 import android
.content
.Intent
;
29 import android
.content
.SharedPreferences
;
30 import android
.os
.Bundle
;
31 import android
.preference
.PreferenceManager
;
32 import android
.text
.Editable
;
33 import android
.text
.TextWatcher
;
34 import android
.view
.KeyEvent
;
35 import android
.view
.View
;
36 import android
.view
.View
.OnClickListener
;
37 import android
.view
.View
.OnFocusChangeListener
;
38 import android
.view
.View
.OnKeyListener
;
39 import android
.widget
.Button
;
40 import android
.widget
.EditText
;
41 import android
.widget
.TextView
;
44 public class PinCodeActivity
extends SherlockFragmentActivity
{
47 public final static String EXTRA_ACTIVITY
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
48 public final static String EXTRA_NEW_STATE
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
57 String
[] tempText
={"","","",""};
61 boolean confirmingPinCode
= false
;
62 boolean pinCodeChecked
= false
;
63 boolean newPasswordEntered
= false
;
64 boolean bChange
= true
; // to control that only one blocks jump
65 int tCounter
; // Count the number of attempts an user could introduce the PIN code
68 protected void onCreate(Bundle savedInstanceState
) {
69 super.onCreate(savedInstanceState
);
70 setContentView(R
.layout
.pincodelock
);
72 Intent intent
= getIntent();
73 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
75 bCancel
= (Button
) findViewById(R
.id
.cancel
);
76 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
77 mText1
= (EditText
) findViewById(R
.id
.txt1
);
78 mText1
.requestFocus();
79 mText2
= (EditText
) findViewById(R
.id
.txt2
);
80 mText3
= (EditText
) findViewById(R
.id
.txt3
);
81 mText4
= (EditText
) findViewById(R
.id
.txt4
);
84 SharedPreferences appPrefs
= PreferenceManager
85 .getDefaultSharedPreferences(getApplicationContext());
88 // Not PIN Code defined yet.
89 // In a previous version settings is allow from start
90 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
92 pinCodeChecked
= true
;
93 newPasswordEntered
= true
;
97 if (appPrefs
.getBoolean("set_pincode", false
)){
99 if (activity
.equals("preferences")){
100 // PIN has been activated yet
101 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
102 pinCodeChecked
= true
; // No need to check it
103 setChangePincodeView(true
);
106 bCancel
.setVisibility(View
.INVISIBLE
);
107 bCancel
.setVisibility(View
.GONE
);
108 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
109 setChangePincodeView(false
);
114 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
115 pinCodeChecked
= false
;
116 setChangePincodeView(true
);
125 protected void setInitVars(){
126 confirmingPinCode
= false
;
127 pinCodeChecked
= false
;
128 newPasswordEntered
= false
;
132 protected void setInitView(){
133 bCancel
.setVisibility(View
.INVISIBLE
);
134 bCancel
.setVisibility(View
.GONE
);
135 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
139 protected void setChangePincodeView(boolean state
){
142 bCancel
.setVisibility(View
.VISIBLE
);
143 bCancel
.setOnClickListener(new OnClickListener() {
145 public void onClick(View v
) {
147 SharedPreferences
.Editor appPrefsE
= PreferenceManager
148 .getDefaultSharedPreferences(getApplicationContext()).edit();
150 SharedPreferences appPrefs
= PreferenceManager
151 .getDefaultSharedPreferences(getApplicationContext());
153 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
154 appPrefsE
.putBoolean("set_pincode",!state
);
169 protected void setTextListeners(){
171 /*------------------------------------------------
173 -------------------------------------------------*/
175 mText1
.addTextChangedListener(new TextWatcher() {
178 public void onTextChanged(CharSequence s
, int start
, int before
,
180 // TODO Auto-generated method stub
181 if (s
.length() > 0) {
182 if (!confirmingPinCode
){
183 tempText
[0] = mText1
.getText().toString();
187 mText2
.requestFocus();
193 public void beforeTextChanged(CharSequence s
, int start
, int count
,
195 // TODO Auto-generated method stub
200 public void afterTextChanged(Editable s
) {
201 // TODO Auto-generated method stub
208 /*------------------------------------------------
210 -------------------------------------------------*/
211 mText2
.addTextChangedListener(new TextWatcher() {
214 public void onTextChanged(CharSequence s
, int start
, int before
,
216 // TODO Auto-generated method stub
217 if (s
.length() > 0) {
218 if (!confirmingPinCode
){
219 tempText
[1] = mText2
.getText().toString();
222 mText3
.requestFocus();
228 public void beforeTextChanged(CharSequence s
, int start
, int count
,
230 // TODO Auto-generated method stub
235 public void afterTextChanged(Editable s
) {
236 // TODO Auto-generated method stub
241 mText2
.setOnKeyListener(new OnKeyListener() {
244 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
245 // TODO Auto-generated method stub
247 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
250 mText1
.requestFocus();
251 if (!confirmingPinCode
)
263 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
266 public void onFocusChange(View v
, boolean hasFocus
) {
267 // TODO Auto-generated method stub
268 if (mText1
.getText().toString().equals("")){
269 mText1
.requestFocus();
278 /*------------------------------------------------
280 -------------------------------------------------*/
281 mText3
.addTextChangedListener(new TextWatcher() {
284 public void onTextChanged(CharSequence s
, int start
, int before
,
286 // TODO Auto-generated method stub
287 if (s
.length() > 0) {
288 if (!confirmingPinCode
){
289 tempText
[2] = mText3
.getText().toString();
291 mText4
.requestFocus();
297 public void beforeTextChanged(CharSequence s
, int start
, int count
,
299 // TODO Auto-generated method stub
304 public void afterTextChanged(Editable s
) {
305 // TODO Auto-generated method stub
310 mText3
.setOnKeyListener(new OnKeyListener() {
313 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
314 // TODO Auto-generated method stub
316 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
317 mText2
.requestFocus();
318 if (!confirmingPinCode
)
331 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
334 public void onFocusChange(View v
, boolean hasFocus
) {
335 // TODO Auto-generated method stub
336 if (mText1
.getText().toString().equals("")){
337 mText1
.requestFocus();
338 }else if (mText2
.getText().toString().equals("")){
339 mText2
.requestFocus();
345 /*------------------------------------------------
347 -------------------------------------------------*/
348 mText4
.addTextChangedListener(new TextWatcher() {
351 public void onTextChanged(CharSequence s
, int start
, int before
,
354 if (s
.length() > 0) {
356 if (!confirmingPinCode
){
357 tempText
[3] = mText4
.getText().toString();
359 mText1
.requestFocus();
361 if (!pinCodeChecked
){
362 pinCodeChecked
= checkPincode();
365 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
367 } else if (pinCodeChecked
){
369 Intent intent
= getIntent();
370 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
372 if (newState
.equals("false")){
373 SharedPreferences
.Editor appPrefs
= PreferenceManager
374 .getDefaultSharedPreferences(getApplicationContext()).edit();
375 appPrefs
.putBoolean("set_pincode",false
);
383 if (!confirmingPinCode
){
384 pinCodeChangeRequest();
398 public void beforeTextChanged(CharSequence s
, int start
, int count
,
400 // TODO Auto-generated method stub
405 public void afterTextChanged(Editable s
) {
406 // TODO Auto-generated method stub
413 mText4
.setOnKeyListener(new OnKeyListener() {
416 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
417 // TODO Auto-generated method stub
419 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
420 mText3
.requestFocus();
421 if (!confirmingPinCode
)
433 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
436 public void onFocusChange(View v
, boolean hasFocus
) {
437 // TODO Auto-generated method stub
438 if (mText1
.getText().toString().equals("")){
439 mText1
.requestFocus();
440 }else if (mText2
.getText().toString().equals("")){
441 mText2
.requestFocus();
442 }else if (mText3
.getText().toString().equals("")){
443 mText3
.requestFocus();
451 } // end setTextListener
454 protected void pinCodeChangeRequest(){
457 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
458 confirmingPinCode
=true
;
463 protected boolean checkPincode(){
466 SharedPreferences appPrefs
= PreferenceManager
467 .getDefaultSharedPreferences(getApplicationContext());
469 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
470 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
471 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
472 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
474 if ( tempText
[0].equals(pText1
) &&
475 tempText
[1].equals(pText2
) &&
476 tempText
[2].equals(pText3
) &&
477 tempText
[3].equals(pText4
) ) {
483 Arrays
.fill(tempText
, null
);
484 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
485 aDialog
.setTitle("ERROR");
486 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
487 aDialog
.setMessage(cseq
);
488 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
491 public void onClick(DialogInterface dialog
, int which
) {
492 // TODO Auto-generated method stub("");
499 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
500 newPasswordEntered
= true
;
501 confirmingPinCode
= false
;
509 protected void confirmPincode(){
511 confirmingPinCode
= false
;
513 String rText1
= mText1
.getText().toString();
514 String rText2
= mText2
.getText().toString();
515 String rText3
= mText3
.getText().toString();
516 String rText4
= mText4
.getText().toString();
518 if ( tempText
[0].equals(rText1
) &&
519 tempText
[1].equals(rText2
) &&
520 tempText
[2].equals(rText3
) &&
521 tempText
[3].equals(rText4
) ) {
523 savePincodeAndExit();
527 Arrays
.fill(tempText
, null
);
528 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
529 aDialog
.setTitle("ERROR");
530 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
531 aDialog
.setMessage(cseq
);
532 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
535 public void onClick(DialogInterface dialog
, int which
) {
536 // TODO Auto-generated method stub("");
542 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
549 protected void pinCodeEnd(boolean state
){
550 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
553 aDialog
.setTitle("SAVE & EXIT");
554 aDialog
.setMessage("PIN Code Activated");
556 aDialog
.setTitle("SAVE & EXIT");
557 aDialog
.setMessage("PIN Code Removed");
560 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
563 public void onClick(DialogInterface dialog
, int which
) {
564 // TODO Auto-generated method stub("");
573 protected void savePincodeAndExit(){
574 SharedPreferences
.Editor appPrefs
= PreferenceManager
575 .getDefaultSharedPreferences(getApplicationContext()).edit();
577 appPrefs
.putString("PrefPinCode1", tempText
[0]);
578 appPrefs
.putString("PrefPinCode2",tempText
[1]);
579 appPrefs
.putString("PrefPinCode3", tempText
[2]);
580 appPrefs
.putString("PrefPinCode4", tempText
[3]);
581 appPrefs
.putBoolean("set_pincode",true
);
591 protected void clearBoxes(){
597 mText1
.requestFocus();
602 public boolean onKeyDown(int keyCode
, KeyEvent event
){
603 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
605 if (activity
.equals("preferences")){
606 SharedPreferences
.Editor appPrefsE
= PreferenceManager
608 .getDefaultSharedPreferences(getApplicationContext()).edit();
610 SharedPreferences appPrefs
= PreferenceManager
611 .getDefaultSharedPreferences(getApplicationContext());
613 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
614 appPrefsE
.putBoolean("set_pincode",!state
);
623 return super.onKeyDown(keyCode
, event
);