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
) ){
99 setChangePincodeView(true
);
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
,
193 public void beforeTextChanged(CharSequence s
, int start
, int count
,
198 public void afterTextChanged(Editable s
) {
199 if (s
.length() > 0) {
200 if (!confirmingPinCode
){
201 tempText
[0] = mText1
.getText().toString();
204 mText2
.requestFocus();
211 /*------------------------------------------------
213 -------------------------------------------------*/
214 mText2
.addTextChangedListener(new TextWatcher() {
217 public void onTextChanged(CharSequence s
, int start
, int before
,
222 public void beforeTextChanged(CharSequence s
, int start
, int count
,
227 public void afterTextChanged(Editable s
) {
228 if (s
.length() > 0) {
229 if (!confirmingPinCode
){
230 tempText
[1] = mText2
.getText().toString();
233 mText3
.requestFocus();
238 mText2
.setOnKeyListener(new OnKeyListener() {
241 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
242 // TODO Auto-generated method stub
244 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
247 mText1
.requestFocus();
248 if (!confirmingPinCode
)
260 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
263 public void onFocusChange(View v
, boolean hasFocus
) {
264 // TODO Auto-generated method stub
266 mText2
.setCursorVisible(true
);
267 if (mText1
.getText().toString().equals("")){
268 mText2
.setSelected(false
);
269 mText2
.setCursorVisible(false
);
270 mText1
.requestFocus();
271 mText1
.setSelected(true
);
272 mText1
.setSelection(0);
279 /*------------------------------------------------
281 -------------------------------------------------*/
282 mText3
.addTextChangedListener(new TextWatcher() {
285 public void onTextChanged(CharSequence s
, int start
, int before
,
290 public void beforeTextChanged(CharSequence s
, int start
, int count
,
295 public void afterTextChanged(Editable s
) {
296 if (s
.length() > 0) {
297 if (!confirmingPinCode
){
298 tempText
[2] = mText3
.getText().toString();
300 mText4
.requestFocus();
305 mText3
.setOnKeyListener(new OnKeyListener() {
308 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
309 // TODO Auto-generated method stub
311 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
312 mText2
.requestFocus();
313 if (!confirmingPinCode
)
326 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
329 public void onFocusChange(View v
, boolean hasFocus
) {
330 // TODO Auto-generated method stub
331 mText3
.setCursorVisible(true
);
332 if (mText1
.getText().toString().equals("")){
333 mText3
.setSelected(false
);
334 mText3
.setCursorVisible(false
);
335 mText1
.requestFocus();
336 mText1
.setSelected(true
);
337 mText1
.setSelection(0);
338 }else if (mText2
.getText().toString().equals("")){
339 mText3
.setSelected(false
);
340 mText3
.setCursorVisible(false
);
341 mText2
.requestFocus();
342 mText2
.setSelected(true
);
343 mText2
.setSelection(0);
349 /*------------------------------------------------
351 -------------------------------------------------*/
352 mText4
.addTextChangedListener(new TextWatcher() {
355 public void onTextChanged(CharSequence s
, int start
, int before
,
360 public void beforeTextChanged(CharSequence s
, int start
, int count
,
365 public void afterTextChanged(Editable s
) {
366 if (s
.length() > 0) {
368 if (!confirmingPinCode
){
369 tempText
[3] = mText4
.getText().toString();
371 mText1
.requestFocus();
373 if (!pinCodeChecked
){
374 pinCodeChecked
= checkPincode();
377 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
379 } else if (pinCodeChecked
){
381 Intent intent
= getIntent();
382 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
384 if (newState
.equals("false")){
385 SharedPreferences
.Editor appPrefs
= PreferenceManager
386 .getDefaultSharedPreferences(getApplicationContext()).edit();
387 appPrefs
.putBoolean("set_pincode",false
);
395 if (!confirmingPinCode
){
396 pinCodeChangeRequest();
411 mText4
.setOnKeyListener(new OnKeyListener() {
414 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
415 // TODO Auto-generated method stub
417 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
418 mText3
.requestFocus();
419 if (!confirmingPinCode
)
431 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
434 public void onFocusChange(View v
, boolean hasFocus
) {
435 // TODO Auto-generated method stub
437 mText4
.setCursorVisible(true
);
439 if (mText1
.getText().toString().equals("")){
440 mText4
.setSelected(false
);
441 mText4
.setCursorVisible(false
);
442 mText1
.requestFocus();
443 mText1
.setSelected(true
);
444 mText1
.setSelection(0);
445 }else if (mText2
.getText().toString().equals("")){
446 mText4
.setSelected(false
);
447 mText4
.setCursorVisible(false
);
448 mText2
.requestFocus();
449 mText2
.setSelected(true
);
450 mText2
.setSelection(0);
451 }else if (mText3
.getText().toString().equals("")){
452 mText4
.setSelected(false
);
453 mText4
.setCursorVisible(false
);
454 mText3
.requestFocus();
455 mText3
.setSelected(true
);
456 mText3
.setSelection(0);
464 } // end setTextListener
467 protected void pinCodeChangeRequest(){
470 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
471 confirmingPinCode
=true
;
476 protected boolean checkPincode(){
479 SharedPreferences appPrefs
= PreferenceManager
480 .getDefaultSharedPreferences(getApplicationContext());
482 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
483 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
484 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
485 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
487 if ( tempText
[0].equals(pText1
) &&
488 tempText
[1].equals(pText2
) &&
489 tempText
[2].equals(pText3
) &&
490 tempText
[3].equals(pText4
) ) {
496 Arrays
.fill(tempText
, null
);
497 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
498 CharSequence errorSeq
= getString(R
.string
.common_error
);
499 aDialog
.setTitle(errorSeq
);
500 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
501 aDialog
.setMessage(cseq
);
502 CharSequence okSeq
= getString(R
.string
.common_ok
);
503 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
506 public void onClick(DialogInterface dialog
, int which
) {
507 // TODO Auto-generated method stub("");
514 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
515 newPasswordEntered
= true
;
516 confirmingPinCode
= false
;
524 protected void confirmPincode(){
526 confirmingPinCode
= false
;
528 String rText1
= mText1
.getText().toString();
529 String rText2
= mText2
.getText().toString();
530 String rText3
= mText3
.getText().toString();
531 String rText4
= mText4
.getText().toString();
533 if ( tempText
[0].equals(rText1
) &&
534 tempText
[1].equals(rText2
) &&
535 tempText
[2].equals(rText3
) &&
536 tempText
[3].equals(rText4
) ) {
538 savePincodeAndExit();
542 Arrays
.fill(tempText
, null
);
543 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
544 CharSequence errorSeq
= getString(R
.string
.common_error
);
545 aDialog
.setTitle(errorSeq
);
546 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
547 aDialog
.setMessage(cseq
);
548 CharSequence okSeq
= getString(R
.string
.common_ok
);
549 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
552 public void onClick(DialogInterface dialog
, int which
) {
553 // TODO Auto-generated method stub("");
559 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
566 protected void pinCodeEnd(boolean state
){
567 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
570 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
571 aDialog
.setTitle(saveSeq
);
572 CharSequence cseq
= getString(R
.string
.pincode_stored
);
573 aDialog
.setMessage(cseq
);
576 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
577 aDialog
.setTitle(saveSeq
);
578 CharSequence cseq
= getString(R
.string
.pincode_removed
);
579 aDialog
.setMessage(cseq
);
582 CharSequence okSeq
= getString(R
.string
.common_ok
);
583 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
586 public void onClick(DialogInterface dialog
, int which
) {
587 // TODO Auto-generated method stub("");
596 protected void savePincodeAndExit(){
597 SharedPreferences
.Editor appPrefs
= PreferenceManager
598 .getDefaultSharedPreferences(getApplicationContext()).edit();
600 appPrefs
.putString("PrefPinCode1", tempText
[0]);
601 appPrefs
.putString("PrefPinCode2",tempText
[1]);
602 appPrefs
.putString("PrefPinCode3", tempText
[2]);
603 appPrefs
.putString("PrefPinCode4", tempText
[3]);
604 appPrefs
.putBoolean("set_pincode",true
);
614 protected void clearBoxes(){
620 mText1
.requestFocus();
625 public boolean onKeyDown(int keyCode
, KeyEvent event
){
626 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
628 if (activity
.equals("preferences")){
629 SharedPreferences
.Editor appPrefsE
= PreferenceManager
631 .getDefaultSharedPreferences(getApplicationContext()).edit();
633 SharedPreferences appPrefs
= PreferenceManager
634 .getDefaultSharedPreferences(getApplicationContext());
636 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
637 appPrefsE
.putBoolean("set_pincode",!state
);
646 return super.onKeyDown(keyCode
, event
);