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
;
27 import android
.app
.AlertDialog
;
28 import android
.content
.DialogInterface
;
29 import android
.content
.Intent
;
30 import android
.content
.SharedPreferences
;
31 import android
.graphics
.Typeface
;
32 import android
.os
.Bundle
;
33 import android
.os
.Handler
;
34 import android
.preference
.PreferenceManager
;
35 import android
.text
.Editable
;
36 import android
.text
.InputType
;
37 import android
.text
.TextWatcher
;
38 import android
.text
.method
.PasswordTransformationMethod
;
39 import android
.view
.KeyEvent
;
40 import android
.view
.View
;
41 import android
.view
.View
.OnClickListener
;
42 import android
.view
.View
.OnFocusChangeListener
;
43 import android
.view
.View
.OnKeyListener
;
44 import android
.view
.ViewGroup
;
45 import android
.widget
.Button
;
46 import android
.widget
.EditText
;
47 import android
.widget
.TextView
;
50 public class PinCodeActivity
extends SherlockFragmentActivity
{
53 public final static String EXTRA_ACTIVITY
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
54 public final static String EXTRA_NEW_STATE
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
63 String
[] tempText
={"","","",""};
67 boolean confirmingPinCode
= false
;
68 boolean pinCodeChecked
= false
;
69 boolean newPasswordEntered
= false
;
70 boolean bChange
= true
; // to control that only one blocks jump
71 int tCounter
; // Count the number of attempts an user could introduce the PIN code
74 protected void onCreate(Bundle savedInstanceState
) {
75 super.onCreate(savedInstanceState
);
76 setContentView(R
.layout
.pincodelock
);
78 Intent intent
= getIntent();
79 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
81 bCancel
= (Button
) findViewById(R
.id
.cancel
);
82 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
83 mText1
= (EditText
) findViewById(R
.id
.txt1
);
84 mText1
.requestFocus();
85 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
86 mText2
= (EditText
) findViewById(R
.id
.txt2
);
87 mText3
= (EditText
) findViewById(R
.id
.txt3
);
88 mText4
= (EditText
) findViewById(R
.id
.txt4
);
92 SharedPreferences appPrefs
= PreferenceManager
93 .getDefaultSharedPreferences(getApplicationContext());
96 // Not PIN Code defined yet.
97 // In a previous version settings is allow from start
98 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
100 pinCodeChecked
= true
;
101 newPasswordEntered
= true
;
105 if (appPrefs
.getBoolean("set_pincode", false
)){
107 if (activity
.equals("preferences")){
108 // PIN has been activated yet
109 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
110 pinCodeChecked
= true
; // No need to check it
111 setChangePincodeView(true
);
114 bCancel
.setVisibility(View
.INVISIBLE
);
115 bCancel
.setVisibility(View
.GONE
);
116 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
117 setChangePincodeView(false
);
122 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
123 pinCodeChecked
= false
;
124 setChangePincodeView(true
);
135 protected void setInitVars(){
136 confirmingPinCode
= false
;
137 pinCodeChecked
= false
;
138 newPasswordEntered
= false
;
142 protected void setInitView(){
143 bCancel
.setVisibility(View
.INVISIBLE
);
144 bCancel
.setVisibility(View
.GONE
);
145 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
149 protected void setChangePincodeView(boolean state
){
152 bCancel
.setVisibility(View
.VISIBLE
);
153 bCancel
.setOnClickListener(new OnClickListener() {
155 public void onClick(View v
) {
157 SharedPreferences
.Editor appPrefsE
= PreferenceManager
158 .getDefaultSharedPreferences(getApplicationContext()).edit();
160 SharedPreferences appPrefs
= PreferenceManager
161 .getDefaultSharedPreferences(getApplicationContext());
163 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
164 appPrefsE
.putBoolean("set_pincode",!state
);
179 protected void setTextListeners(){
181 /*------------------------------------------------
183 -------------------------------------------------*/
185 mText1
.addTextChangedListener(new TextWatcher() {
188 public void onTextChanged(CharSequence s
, int start
, int before
,
190 // TODO Auto-generated method stub
191 if (s
.length() > 0) {
192 if (!confirmingPinCode
){
193 tempText
[0] = mText1
.getText().toString();
197 mText1
.setTransformationMethod(new PasswordTransformationMethod());
198 mText2
.requestFocus();
205 public void beforeTextChanged(CharSequence s
, int start
, int count
,
207 // TODO Auto-generated method stub
212 public void afterTextChanged(Editable s
) {
213 // TODO Auto-generated method stub
221 /*------------------------------------------------
223 -------------------------------------------------*/
224 mText2
.addTextChangedListener(new TextWatcher() {
227 public void onTextChanged(CharSequence s
, int start
, int before
,
229 // TODO Auto-generated method stub
230 if (s
.length() > 0) {
231 if (!confirmingPinCode
){
232 tempText
[1] = mText2
.getText().toString();
235 mText3
.requestFocus();
241 public void beforeTextChanged(CharSequence s
, int start
, int count
,
243 // TODO Auto-generated method stub
248 public void afterTextChanged(Editable s
) {
249 // TODO Auto-generated method stub
254 mText2
.setOnKeyListener(new OnKeyListener() {
257 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
258 // TODO Auto-generated method stub
260 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
263 mText1
.requestFocus();
264 if (!confirmingPinCode
)
276 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
279 public void onFocusChange(View v
, boolean hasFocus
) {
280 // TODO Auto-generated method stub
282 if (mText1
.getText().toString().equals("")){
283 mText1
.requestFocus();
290 /*------------------------------------------------
292 -------------------------------------------------*/
293 mText3
.addTextChangedListener(new TextWatcher() {
296 public void onTextChanged(CharSequence s
, int start
, int before
,
298 // TODO Auto-generated method stub
299 if (s
.length() > 0) {
300 if (!confirmingPinCode
){
301 tempText
[2] = mText3
.getText().toString();
303 mText4
.requestFocus();
309 public void beforeTextChanged(CharSequence s
, int start
, int count
,
311 // TODO Auto-generated method stub
316 public void afterTextChanged(Editable s
) {
317 // TODO Auto-generated method stub
322 mText3
.setOnKeyListener(new OnKeyListener() {
325 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
326 // TODO Auto-generated method stub
328 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
329 mText2
.requestFocus();
330 if (!confirmingPinCode
)
343 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
346 public void onFocusChange(View v
, boolean hasFocus
) {
347 // TODO Auto-generated method stub
349 if (mText1
.getText().toString().equals("")){
350 mText1
.requestFocus();
351 }else if (mText2
.getText().toString().equals("")){
352 mText2
.requestFocus();
358 /*------------------------------------------------
360 -------------------------------------------------*/
361 mText4
.addTextChangedListener(new TextWatcher() {
364 public void onTextChanged(CharSequence s
, int start
, int before
,
367 if (s
.length() > 0) {
369 if (!confirmingPinCode
){
370 tempText
[3] = mText4
.getText().toString();
372 mText1
.requestFocus();
374 if (!pinCodeChecked
){
375 pinCodeChecked
= checkPincode();
378 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
380 } else if (pinCodeChecked
){
382 Intent intent
= getIntent();
383 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
385 if (newState
.equals("false")){
386 SharedPreferences
.Editor appPrefs
= PreferenceManager
387 .getDefaultSharedPreferences(getApplicationContext()).edit();
388 appPrefs
.putBoolean("set_pincode",false
);
396 if (!confirmingPinCode
){
397 pinCodeChangeRequest();
411 public void beforeTextChanged(CharSequence s
, int start
, int count
,
413 // TODO Auto-generated method stub
418 public void afterTextChanged(Editable s
) {
419 // TODO Auto-generated method stub
426 mText4
.setOnKeyListener(new OnKeyListener() {
429 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
430 // TODO Auto-generated method stub
432 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
433 mText3
.requestFocus();
434 if (!confirmingPinCode
)
446 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
449 public void onFocusChange(View v
, boolean hasFocus
) {
450 // TODO Auto-generated method stub
452 if (mText1
.getText().toString().equals("")){
453 mText1
.requestFocus();
454 }else if (mText2
.getText().toString().equals("")){
455 mText2
.requestFocus();
456 }else if (mText3
.getText().toString().equals("")){
457 mText3
.requestFocus();
465 } // end setTextListener
468 protected void pinCodeChangeRequest(){
471 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
472 confirmingPinCode
=true
;
477 protected boolean checkPincode(){
480 SharedPreferences appPrefs
= PreferenceManager
481 .getDefaultSharedPreferences(getApplicationContext());
483 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
484 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
485 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
486 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
488 if ( tempText
[0].equals(pText1
) &&
489 tempText
[1].equals(pText2
) &&
490 tempText
[2].equals(pText3
) &&
491 tempText
[3].equals(pText4
) ) {
497 Arrays
.fill(tempText
, null
);
498 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
499 CharSequence errorSeq
= getString(R
.string
.common_error
);
500 aDialog
.setTitle(errorSeq
);
501 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
502 aDialog
.setMessage(cseq
);
503 CharSequence okSeq
= getString(R
.string
.common_ok
);
504 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
507 public void onClick(DialogInterface dialog
, int which
) {
508 // TODO Auto-generated method stub("");
515 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
516 newPasswordEntered
= true
;
517 confirmingPinCode
= false
;
525 protected void confirmPincode(){
527 confirmingPinCode
= false
;
529 String rText1
= mText1
.getText().toString();
530 String rText2
= mText2
.getText().toString();
531 String rText3
= mText3
.getText().toString();
532 String rText4
= mText4
.getText().toString();
534 if ( tempText
[0].equals(rText1
) &&
535 tempText
[1].equals(rText2
) &&
536 tempText
[2].equals(rText3
) &&
537 tempText
[3].equals(rText4
) ) {
539 savePincodeAndExit();
543 Arrays
.fill(tempText
, null
);
544 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
545 CharSequence errorSeq
= getString(R
.string
.common_error
);
546 aDialog
.setTitle(errorSeq
);
547 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
548 aDialog
.setMessage(cseq
);
549 CharSequence okSeq
= getString(R
.string
.common_ok
);
550 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
553 public void onClick(DialogInterface dialog
, int which
) {
554 // TODO Auto-generated method stub("");
560 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
567 protected void pinCodeEnd(boolean state
){
568 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
571 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
572 aDialog
.setTitle(saveSeq
);
573 CharSequence cseq
= getString(R
.string
.pincode_stored
);
574 aDialog
.setMessage(cseq
);
577 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
578 aDialog
.setTitle(saveSeq
);
579 CharSequence cseq
= getString(R
.string
.pincode_removed
);
580 aDialog
.setMessage(cseq
);
583 CharSequence okSeq
= getString(R
.string
.common_ok
);
584 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
587 public void onClick(DialogInterface dialog
, int which
) {
588 // TODO Auto-generated method stub("");
597 protected void savePincodeAndExit(){
598 SharedPreferences
.Editor appPrefs
= PreferenceManager
599 .getDefaultSharedPreferences(getApplicationContext()).edit();
601 appPrefs
.putString("PrefPinCode1", tempText
[0]);
602 appPrefs
.putString("PrefPinCode2",tempText
[1]);
603 appPrefs
.putString("PrefPinCode3", tempText
[2]);
604 appPrefs
.putString("PrefPinCode4", tempText
[3]);
605 appPrefs
.putBoolean("set_pincode",true
);
615 protected void clearBoxes(){
621 mText1
.requestFocus();
626 public boolean onKeyDown(int keyCode
, KeyEvent event
){
627 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
629 if (activity
.equals("preferences")){
630 SharedPreferences
.Editor appPrefsE
= PreferenceManager
632 .getDefaultSharedPreferences(getApplicationContext()).edit();
634 SharedPreferences appPrefs
= PreferenceManager
635 .getDefaultSharedPreferences(getApplicationContext());
637 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
638 appPrefsE
.putBoolean("set_pincode",!state
);
647 return super.onKeyDown(keyCode
, event
);