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 version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package de
.mobilcom
.debitel
.cloud
.android
.ui
.activity
;
19 import java
.util
.Arrays
;
21 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
23 import de
.mobilcom
.debitel
.cloud
.android
.R
;
25 import android
.app
.AlertDialog
;
26 import android
.content
.DialogInterface
;
27 import android
.content
.Intent
;
28 import android
.content
.SharedPreferences
;
29 import android
.os
.Bundle
;
30 import android
.preference
.PreferenceManager
;
31 import android
.text
.Editable
;
32 import android
.text
.TextWatcher
;
33 import android
.view
.KeyEvent
;
34 import android
.view
.View
;
35 import android
.view
.View
.OnClickListener
;
36 import android
.view
.View
.OnFocusChangeListener
;
37 import android
.view
.View
.OnKeyListener
;
38 import android
.widget
.Button
;
39 import android
.widget
.EditText
;
40 import android
.widget
.TextView
;
42 public class PinCodeActivity
extends SherlockFragmentActivity
{
45 public final static String EXTRA_ACTIVITY
= "de.mobilcom.debitel.cloud.android.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE
= "de.mobilcom.debitel.cloud.android.ui.activity.PinCodeActivity.NEW_STATE";
50 TextView mPinHdrExplanation
;
56 String
[] tempText
={"","","",""};
60 boolean confirmingPinCode
= false
;
61 boolean pinCodeChecked
= false
;
62 boolean newPasswordEntered
= false
;
63 boolean bChange
= true
; // to control that only one blocks jump
64 int tCounter
; // Count the number of attempts an user could introduce the PIN code
67 protected void onCreate(Bundle savedInstanceState
) {
68 super.onCreate(savedInstanceState
);
69 setContentView(R
.layout
.pincodelock
);
71 Intent intent
= getIntent();
72 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
74 bCancel
= (Button
) findViewById(R
.id
.cancel
);
75 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
76 mPinHdrExplanation
= (TextView
) findViewById(R
.id
.pinHdrExpl
);
77 mText1
= (EditText
) findViewById(R
.id
.txt1
);
78 mText1
.requestFocus();
79 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
80 mText2
= (EditText
) findViewById(R
.id
.txt2
);
81 mText3
= (EditText
) findViewById(R
.id
.txt3
);
82 mText4
= (EditText
) findViewById(R
.id
.txt4
);
84 // Set background of 'Cancel' button
85 boolean customButtons
= getResources().getBoolean(R
.bool
.custom_buttons
);
87 bCancel
.setBackgroundResource(R
.drawable
.btn_default
);
89 SharedPreferences appPrefs
= PreferenceManager
90 .getDefaultSharedPreferences(getApplicationContext());
93 // Not PIN Code defined yet.
94 // In a previous version settings is allow from start
95 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
96 setChangePincodeView(true
);
97 pinCodeChecked
= true
;
98 newPasswordEntered
= true
;
102 if (appPrefs
.getBoolean("set_pincode", false
)){
104 if (activity
.equals("preferences")){
105 // PIN has been activated yet
106 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
107 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
108 pinCodeChecked
= true
; // No need to check it
109 setChangePincodeView(true
);
112 bCancel
.setVisibility(View
.INVISIBLE
);
113 bCancel
.setVisibility(View
.GONE
);
114 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
115 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
116 setChangePincodeView(false
);
121 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
122 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
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
);
146 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
150 protected void setChangePincodeView(boolean state
){
153 bCancel
.setVisibility(View
.VISIBLE
);
154 bCancel
.setOnClickListener(new OnClickListener() {
156 public void onClick(View v
) {
158 SharedPreferences
.Editor appPrefsE
= PreferenceManager
159 .getDefaultSharedPreferences(getApplicationContext()).edit();
161 SharedPreferences appPrefs
= PreferenceManager
162 .getDefaultSharedPreferences(getApplicationContext());
164 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
165 appPrefsE
.putBoolean("set_pincode",!state
);
180 protected void setTextListeners(){
182 /*------------------------------------------------
184 -------------------------------------------------*/
186 mText1
.addTextChangedListener(new TextWatcher() {
189 public void onTextChanged(CharSequence s
, int start
, int before
,
194 public void beforeTextChanged(CharSequence s
, int start
, int count
,
199 public void afterTextChanged(Editable s
) {
200 if (s
.length() > 0) {
201 if (!confirmingPinCode
){
202 tempText
[0] = mText1
.getText().toString();
205 mText2
.requestFocus();
212 /*------------------------------------------------
214 -------------------------------------------------*/
215 mText2
.addTextChangedListener(new TextWatcher() {
218 public void onTextChanged(CharSequence s
, int start
, int before
,
223 public void beforeTextChanged(CharSequence s
, int start
, int count
,
228 public void afterTextChanged(Editable s
) {
229 if (s
.length() > 0) {
230 if (!confirmingPinCode
){
231 tempText
[1] = mText2
.getText().toString();
234 mText3
.requestFocus();
239 mText2
.setOnKeyListener(new OnKeyListener() {
242 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
243 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
246 mText1
.requestFocus();
247 if (!confirmingPinCode
)
259 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
262 public void onFocusChange(View v
, boolean hasFocus
) {
263 mText2
.setCursorVisible(true
);
264 if (mText1
.getText().toString().equals("")){
265 mText2
.setSelected(false
);
266 mText2
.setCursorVisible(false
);
267 mText1
.requestFocus();
268 mText1
.setSelected(true
);
269 mText1
.setSelection(0);
276 /*------------------------------------------------
278 -------------------------------------------------*/
279 mText3
.addTextChangedListener(new TextWatcher() {
282 public void onTextChanged(CharSequence s
, int start
, int before
,
287 public void beforeTextChanged(CharSequence s
, int start
, int count
,
292 public void afterTextChanged(Editable s
) {
293 if (s
.length() > 0) {
294 if (!confirmingPinCode
){
295 tempText
[2] = mText3
.getText().toString();
297 mText4
.requestFocus();
302 mText3
.setOnKeyListener(new OnKeyListener() {
305 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
306 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
307 mText2
.requestFocus();
308 if (!confirmingPinCode
)
321 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
324 public void onFocusChange(View v
, boolean hasFocus
) {
325 mText3
.setCursorVisible(true
);
326 if (mText1
.getText().toString().equals("")){
327 mText3
.setSelected(false
);
328 mText3
.setCursorVisible(false
);
329 mText1
.requestFocus();
330 mText1
.setSelected(true
);
331 mText1
.setSelection(0);
332 }else if (mText2
.getText().toString().equals("")){
333 mText3
.setSelected(false
);
334 mText3
.setCursorVisible(false
);
335 mText2
.requestFocus();
336 mText2
.setSelected(true
);
337 mText2
.setSelection(0);
343 /*------------------------------------------------
345 -------------------------------------------------*/
346 mText4
.addTextChangedListener(new TextWatcher() {
349 public void onTextChanged(CharSequence s
, int start
, int before
,
354 public void beforeTextChanged(CharSequence s
, int start
, int count
,
359 public void afterTextChanged(Editable s
) {
360 if (s
.length() > 0) {
362 if (!confirmingPinCode
){
363 tempText
[3] = mText4
.getText().toString();
365 mText1
.requestFocus();
367 if (!pinCodeChecked
){
368 pinCodeChecked
= checkPincode();
371 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
373 } else if (pinCodeChecked
){
375 Intent intent
= getIntent();
376 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
378 if (newState
.equals("false")){
379 SharedPreferences
.Editor appPrefs
= PreferenceManager
380 .getDefaultSharedPreferences(getApplicationContext()).edit();
381 appPrefs
.putBoolean("set_pincode",false
);
389 if (!confirmingPinCode
){
390 pinCodeChangeRequest();
405 mText4
.setOnKeyListener(new OnKeyListener() {
408 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
409 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
410 mText3
.requestFocus();
411 if (!confirmingPinCode
)
423 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
426 public void onFocusChange(View v
, boolean hasFocus
) {
427 mText4
.setCursorVisible(true
);
429 if (mText1
.getText().toString().equals("")){
430 mText4
.setSelected(false
);
431 mText4
.setCursorVisible(false
);
432 mText1
.requestFocus();
433 mText1
.setSelected(true
);
434 mText1
.setSelection(0);
435 }else if (mText2
.getText().toString().equals("")){
436 mText4
.setSelected(false
);
437 mText4
.setCursorVisible(false
);
438 mText2
.requestFocus();
439 mText2
.setSelected(true
);
440 mText2
.setSelection(0);
441 }else if (mText3
.getText().toString().equals("")){
442 mText4
.setSelected(false
);
443 mText4
.setCursorVisible(false
);
444 mText3
.requestFocus();
445 mText3
.setSelected(true
);
446 mText3
.setSelection(0);
454 } // end setTextListener
457 protected void pinCodeChangeRequest(){
460 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
461 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
462 confirmingPinCode
=true
;
467 protected boolean checkPincode(){
470 SharedPreferences appPrefs
= PreferenceManager
471 .getDefaultSharedPreferences(getApplicationContext());
473 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
474 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
475 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
476 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
478 if ( tempText
[0].equals(pText1
) &&
479 tempText
[1].equals(pText2
) &&
480 tempText
[2].equals(pText3
) &&
481 tempText
[3].equals(pText4
) ) {
487 Arrays
.fill(tempText
, null
);
488 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
489 CharSequence errorSeq
= getString(R
.string
.common_error
);
490 aDialog
.setTitle(errorSeq
);
491 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
492 aDialog
.setMessage(cseq
);
493 CharSequence okSeq
= getString(R
.string
.common_ok
);
494 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
497 public void onClick(DialogInterface dialog
, int which
) {
504 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
505 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
506 newPasswordEntered
= true
;
507 confirmingPinCode
= false
;
515 protected void confirmPincode(){
517 confirmingPinCode
= false
;
519 String rText1
= mText1
.getText().toString();
520 String rText2
= mText2
.getText().toString();
521 String rText3
= mText3
.getText().toString();
522 String rText4
= mText4
.getText().toString();
524 if ( tempText
[0].equals(rText1
) &&
525 tempText
[1].equals(rText2
) &&
526 tempText
[2].equals(rText3
) &&
527 tempText
[3].equals(rText4
) ) {
529 savePincodeAndExit();
533 Arrays
.fill(tempText
, null
);
534 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
535 CharSequence errorSeq
= getString(R
.string
.common_error
);
536 aDialog
.setTitle(errorSeq
);
537 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
538 aDialog
.setMessage(cseq
);
539 CharSequence okSeq
= getString(R
.string
.common_ok
);
540 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
543 public void onClick(DialogInterface dialog
, int which
) {
549 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
550 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
557 protected void pinCodeEnd(boolean state
){
558 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
561 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
562 aDialog
.setTitle(saveSeq
);
563 CharSequence cseq
= getString(R
.string
.pincode_stored
);
564 aDialog
.setMessage(cseq
);
567 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
568 aDialog
.setTitle(saveSeq
);
569 CharSequence cseq
= getString(R
.string
.pincode_removed
);
570 aDialog
.setMessage(cseq
);
573 CharSequence okSeq
= getString(R
.string
.common_ok
);
574 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
577 public void onClick(DialogInterface dialog
, int which
) {
586 protected void savePincodeAndExit(){
587 SharedPreferences
.Editor appPrefs
= PreferenceManager
588 .getDefaultSharedPreferences(getApplicationContext()).edit();
590 appPrefs
.putString("PrefPinCode1", tempText
[0]);
591 appPrefs
.putString("PrefPinCode2",tempText
[1]);
592 appPrefs
.putString("PrefPinCode3", tempText
[2]);
593 appPrefs
.putString("PrefPinCode4", tempText
[3]);
594 appPrefs
.putBoolean("set_pincode",true
);
604 protected void clearBoxes(){
610 mText1
.requestFocus();
615 public boolean onKeyDown(int keyCode
, KeyEvent event
){
616 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
618 if (activity
.equals("preferences")){
619 SharedPreferences
.Editor appPrefsE
= PreferenceManager
621 .getDefaultSharedPreferences(getApplicationContext()).edit();
623 SharedPreferences appPrefs
= PreferenceManager
624 .getDefaultSharedPreferences(getApplicationContext());
626 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
627 appPrefsE
.putBoolean("set_pincode",!state
);
636 return super.onKeyDown(keyCode
, event
);