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
,
190 // TODO Auto-generated method stub
191 if (s
.length() > 0) {
192 if (!confirmingPinCode
){
193 tempText
[0] = mText1
.getText().toString();
196 mText2
.requestFocus();
201 public void beforeTextChanged(CharSequence s
, int start
, int count
,
203 // TODO Auto-generated method stub
208 public void afterTextChanged(Editable s
) {
209 // TODO Auto-generated method stub
217 /*------------------------------------------------
219 -------------------------------------------------*/
220 mText2
.addTextChangedListener(new TextWatcher() {
223 public void onTextChanged(CharSequence s
, int start
, int before
,
225 // TODO Auto-generated method stub
226 if (s
.length() > 0) {
227 if (!confirmingPinCode
){
228 tempText
[1] = mText2
.getText().toString();
231 mText3
.requestFocus();
237 public void beforeTextChanged(CharSequence s
, int start
, int count
,
239 // TODO Auto-generated method stub
244 public void afterTextChanged(Editable s
) {
245 // TODO Auto-generated method stub
250 mText2
.setOnKeyListener(new OnKeyListener() {
253 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
254 // TODO Auto-generated method stub
256 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
259 mText1
.requestFocus();
260 if (!confirmingPinCode
)
272 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
275 public void onFocusChange(View v
, boolean hasFocus
) {
276 // TODO Auto-generated method stub
278 mText2
.setCursorVisible(true
);
279 if (mText1
.getText().toString().equals("")){
280 mText2
.setSelected(false
);
281 mText2
.setCursorVisible(false
);
282 mText1
.requestFocus();
283 mText1
.setSelected(true
);
284 mText1
.setSelection(0);
291 /*------------------------------------------------
293 -------------------------------------------------*/
294 mText3
.addTextChangedListener(new TextWatcher() {
297 public void onTextChanged(CharSequence s
, int start
, int before
,
299 // TODO Auto-generated method stub
300 if (s
.length() > 0) {
301 if (!confirmingPinCode
){
302 tempText
[2] = mText3
.getText().toString();
304 mText4
.requestFocus();
310 public void beforeTextChanged(CharSequence s
, int start
, int count
,
312 // TODO Auto-generated method stub
317 public void afterTextChanged(Editable s
) {
318 // TODO Auto-generated method stub
323 mText3
.setOnKeyListener(new OnKeyListener() {
326 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
327 // TODO Auto-generated method stub
329 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
330 mText2
.requestFocus();
331 if (!confirmingPinCode
)
344 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
347 public void onFocusChange(View v
, boolean hasFocus
) {
348 // TODO Auto-generated method stub
349 mText3
.setCursorVisible(true
);
350 if (mText1
.getText().toString().equals("")){
351 mText3
.setSelected(false
);
352 mText3
.setCursorVisible(false
);
353 mText1
.requestFocus();
354 mText1
.setSelected(true
);
355 mText1
.setSelection(0);
356 }else if (mText2
.getText().toString().equals("")){
357 mText3
.setSelected(false
);
358 mText3
.setCursorVisible(false
);
359 mText2
.requestFocus();
360 mText2
.setSelected(true
);
361 mText2
.setSelection(0);
367 /*------------------------------------------------
369 -------------------------------------------------*/
370 mText4
.addTextChangedListener(new TextWatcher() {
373 public void onTextChanged(CharSequence s
, int start
, int before
,
376 if (s
.length() > 0) {
378 if (!confirmingPinCode
){
379 tempText
[3] = mText4
.getText().toString();
381 mText1
.requestFocus();
383 if (!pinCodeChecked
){
384 pinCodeChecked
= checkPincode();
387 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
389 } else if (pinCodeChecked
){
391 Intent intent
= getIntent();
392 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
394 if (newState
.equals("false")){
395 SharedPreferences
.Editor appPrefs
= PreferenceManager
396 .getDefaultSharedPreferences(getApplicationContext()).edit();
397 appPrefs
.putBoolean("set_pincode",false
);
405 if (!confirmingPinCode
){
406 pinCodeChangeRequest();
420 public void beforeTextChanged(CharSequence s
, int start
, int count
,
422 // TODO Auto-generated method stub
427 public void afterTextChanged(Editable s
) {
428 // TODO Auto-generated method stub
435 mText4
.setOnKeyListener(new OnKeyListener() {
438 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
439 // TODO Auto-generated method stub
441 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
442 mText3
.requestFocus();
443 if (!confirmingPinCode
)
455 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
458 public void onFocusChange(View v
, boolean hasFocus
) {
459 // TODO Auto-generated method stub
461 mText4
.setCursorVisible(true
);
463 if (mText1
.getText().toString().equals("")){
464 mText4
.setSelected(false
);
465 mText4
.setCursorVisible(false
);
466 mText1
.requestFocus();
467 mText1
.setSelected(true
);
468 mText1
.setSelection(0);
469 }else if (mText2
.getText().toString().equals("")){
470 mText4
.setSelected(false
);
471 mText4
.setCursorVisible(false
);
472 mText2
.requestFocus();
473 mText2
.setSelected(true
);
474 mText2
.setSelection(0);
475 }else if (mText3
.getText().toString().equals("")){
476 mText4
.setSelected(false
);
477 mText4
.setCursorVisible(false
);
478 mText3
.requestFocus();
479 mText3
.setSelected(true
);
480 mText3
.setSelection(0);
488 } // end setTextListener
491 protected void pinCodeChangeRequest(){
494 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
495 confirmingPinCode
=true
;
500 protected boolean checkPincode(){
503 SharedPreferences appPrefs
= PreferenceManager
504 .getDefaultSharedPreferences(getApplicationContext());
506 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
507 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
508 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
509 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
511 if ( tempText
[0].equals(pText1
) &&
512 tempText
[1].equals(pText2
) &&
513 tempText
[2].equals(pText3
) &&
514 tempText
[3].equals(pText4
) ) {
520 Arrays
.fill(tempText
, null
);
521 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
522 CharSequence errorSeq
= getString(R
.string
.common_error
);
523 aDialog
.setTitle(errorSeq
);
524 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
525 aDialog
.setMessage(cseq
);
526 CharSequence okSeq
= getString(R
.string
.common_ok
);
527 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
530 public void onClick(DialogInterface dialog
, int which
) {
531 // TODO Auto-generated method stub("");
538 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
539 newPasswordEntered
= true
;
540 confirmingPinCode
= false
;
548 protected void confirmPincode(){
550 confirmingPinCode
= false
;
552 String rText1
= mText1
.getText().toString();
553 String rText2
= mText2
.getText().toString();
554 String rText3
= mText3
.getText().toString();
555 String rText4
= mText4
.getText().toString();
557 if ( tempText
[0].equals(rText1
) &&
558 tempText
[1].equals(rText2
) &&
559 tempText
[2].equals(rText3
) &&
560 tempText
[3].equals(rText4
) ) {
562 savePincodeAndExit();
566 Arrays
.fill(tempText
, null
);
567 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
568 CharSequence errorSeq
= getString(R
.string
.common_error
);
569 aDialog
.setTitle(errorSeq
);
570 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
571 aDialog
.setMessage(cseq
);
572 CharSequence okSeq
= getString(R
.string
.common_ok
);
573 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
576 public void onClick(DialogInterface dialog
, int which
) {
577 // TODO Auto-generated method stub("");
583 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
590 protected void pinCodeEnd(boolean state
){
591 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
594 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
595 aDialog
.setTitle(saveSeq
);
596 CharSequence cseq
= getString(R
.string
.pincode_stored
);
597 aDialog
.setMessage(cseq
);
600 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
601 aDialog
.setTitle(saveSeq
);
602 CharSequence cseq
= getString(R
.string
.pincode_removed
);
603 aDialog
.setMessage(cseq
);
606 CharSequence okSeq
= getString(R
.string
.common_ok
);
607 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
610 public void onClick(DialogInterface dialog
, int which
) {
611 // TODO Auto-generated method stub("");
620 protected void savePincodeAndExit(){
621 SharedPreferences
.Editor appPrefs
= PreferenceManager
622 .getDefaultSharedPreferences(getApplicationContext()).edit();
624 appPrefs
.putString("PrefPinCode1", tempText
[0]);
625 appPrefs
.putString("PrefPinCode2",tempText
[1]);
626 appPrefs
.putString("PrefPinCode3", tempText
[2]);
627 appPrefs
.putString("PrefPinCode4", tempText
[3]);
628 appPrefs
.putBoolean("set_pincode",true
);
638 protected void clearBoxes(){
644 mText1
.requestFocus();
649 public boolean onKeyDown(int keyCode
, KeyEvent event
){
650 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
652 if (activity
.equals("preferences")){
653 SharedPreferences
.Editor appPrefsE
= PreferenceManager
655 .getDefaultSharedPreferences(getApplicationContext()).edit();
657 SharedPreferences appPrefs
= PreferenceManager
658 .getDefaultSharedPreferences(getApplicationContext());
660 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
661 appPrefsE
.putBoolean("set_pincode",!state
);
670 return super.onKeyDown(keyCode
, event
);