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 com
.owncloud
.android
.ui
.activity
;
19 import java
.util
.Arrays
;
21 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
22 import com
.owncloud
.android
.R
;
23 import com
.owncloud
.android
.ui
.CustomButton
;
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
.EditText
;
40 import android
.widget
.TextView
;
42 public class PinCodeActivity
extends SherlockFragmentActivity
{
45 public final static String EXTRA_ACTIVITY
= "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE
= "com.owncloud.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
= (CustomButton
) 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 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
) ){
91 setChangePincodeView(true
);
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 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
103 pinCodeChecked
= true
; // No need to check it
104 setChangePincodeView(true
);
107 bCancel
.setVisibility(View
.INVISIBLE
);
108 bCancel
.setVisibility(View
.GONE
);
109 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
110 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
111 setChangePincodeView(false
);
116 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
117 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
118 pinCodeChecked
= false
;
119 setChangePincodeView(true
);
130 protected void setInitVars(){
131 confirmingPinCode
= false
;
132 pinCodeChecked
= false
;
133 newPasswordEntered
= false
;
137 protected void setInitView(){
138 bCancel
.setVisibility(View
.INVISIBLE
);
139 bCancel
.setVisibility(View
.GONE
);
140 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
141 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
145 protected void setChangePincodeView(boolean state
){
148 bCancel
.setVisibility(View
.VISIBLE
);
149 bCancel
.setOnClickListener(new OnClickListener() {
151 public void onClick(View v
) {
153 SharedPreferences
.Editor appPrefsE
= PreferenceManager
154 .getDefaultSharedPreferences(getApplicationContext()).edit();
156 SharedPreferences appPrefs
= PreferenceManager
157 .getDefaultSharedPreferences(getApplicationContext());
159 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
160 appPrefsE
.putBoolean("set_pincode",!state
);
175 protected void setTextListeners(){
177 /*------------------------------------------------
179 -------------------------------------------------*/
181 mText1
.addTextChangedListener(new TextWatcher() {
184 public void onTextChanged(CharSequence s
, int start
, int before
,
189 public void beforeTextChanged(CharSequence s
, int start
, int count
,
194 public void afterTextChanged(Editable s
) {
195 if (s
.length() > 0) {
196 if (!confirmingPinCode
){
197 tempText
[0] = mText1
.getText().toString();
200 mText2
.requestFocus();
207 /*------------------------------------------------
209 -------------------------------------------------*/
210 mText2
.addTextChangedListener(new TextWatcher() {
213 public void onTextChanged(CharSequence s
, int start
, int before
,
218 public void beforeTextChanged(CharSequence s
, int start
, int count
,
223 public void afterTextChanged(Editable s
) {
224 if (s
.length() > 0) {
225 if (!confirmingPinCode
){
226 tempText
[1] = mText2
.getText().toString();
229 mText3
.requestFocus();
234 mText2
.setOnKeyListener(new OnKeyListener() {
237 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
238 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
241 mText1
.requestFocus();
242 if (!confirmingPinCode
)
254 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
257 public void onFocusChange(View v
, boolean hasFocus
) {
258 mText2
.setCursorVisible(true
);
259 if (mText1
.getText().toString().equals("")){
260 mText2
.setSelected(false
);
261 mText2
.setCursorVisible(false
);
262 mText1
.requestFocus();
263 mText1
.setSelected(true
);
264 mText1
.setSelection(0);
271 /*------------------------------------------------
273 -------------------------------------------------*/
274 mText3
.addTextChangedListener(new TextWatcher() {
277 public void onTextChanged(CharSequence s
, int start
, int before
,
282 public void beforeTextChanged(CharSequence s
, int start
, int count
,
287 public void afterTextChanged(Editable s
) {
288 if (s
.length() > 0) {
289 if (!confirmingPinCode
){
290 tempText
[2] = mText3
.getText().toString();
292 mText4
.requestFocus();
297 mText3
.setOnKeyListener(new OnKeyListener() {
300 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
301 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
302 mText2
.requestFocus();
303 if (!confirmingPinCode
)
316 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
319 public void onFocusChange(View v
, boolean hasFocus
) {
320 mText3
.setCursorVisible(true
);
321 if (mText1
.getText().toString().equals("")){
322 mText3
.setSelected(false
);
323 mText3
.setCursorVisible(false
);
324 mText1
.requestFocus();
325 mText1
.setSelected(true
);
326 mText1
.setSelection(0);
327 }else if (mText2
.getText().toString().equals("")){
328 mText3
.setSelected(false
);
329 mText3
.setCursorVisible(false
);
330 mText2
.requestFocus();
331 mText2
.setSelected(true
);
332 mText2
.setSelection(0);
338 /*------------------------------------------------
340 -------------------------------------------------*/
341 mText4
.addTextChangedListener(new TextWatcher() {
344 public void onTextChanged(CharSequence s
, int start
, int before
,
349 public void beforeTextChanged(CharSequence s
, int start
, int count
,
354 public void afterTextChanged(Editable s
) {
355 if (s
.length() > 0) {
357 if (!confirmingPinCode
){
358 tempText
[3] = mText4
.getText().toString();
360 mText1
.requestFocus();
362 if (!pinCodeChecked
){
363 pinCodeChecked
= checkPincode();
366 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
368 } else if (pinCodeChecked
){
370 Intent intent
= getIntent();
371 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
373 if (newState
.equals("false")){
374 SharedPreferences
.Editor appPrefs
= PreferenceManager
375 .getDefaultSharedPreferences(getApplicationContext()).edit();
376 appPrefs
.putBoolean("set_pincode",false
);
384 if (!confirmingPinCode
){
385 pinCodeChangeRequest();
400 mText4
.setOnKeyListener(new OnKeyListener() {
403 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
404 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
405 mText3
.requestFocus();
406 if (!confirmingPinCode
)
418 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
421 public void onFocusChange(View v
, boolean hasFocus
) {
422 mText4
.setCursorVisible(true
);
424 if (mText1
.getText().toString().equals("")){
425 mText4
.setSelected(false
);
426 mText4
.setCursorVisible(false
);
427 mText1
.requestFocus();
428 mText1
.setSelected(true
);
429 mText1
.setSelection(0);
430 }else if (mText2
.getText().toString().equals("")){
431 mText4
.setSelected(false
);
432 mText4
.setCursorVisible(false
);
433 mText2
.requestFocus();
434 mText2
.setSelected(true
);
435 mText2
.setSelection(0);
436 }else if (mText3
.getText().toString().equals("")){
437 mText4
.setSelected(false
);
438 mText4
.setCursorVisible(false
);
439 mText3
.requestFocus();
440 mText3
.setSelected(true
);
441 mText3
.setSelection(0);
449 } // end setTextListener
452 protected void pinCodeChangeRequest(){
455 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
456 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
457 confirmingPinCode
=true
;
462 protected boolean checkPincode(){
465 SharedPreferences appPrefs
= PreferenceManager
466 .getDefaultSharedPreferences(getApplicationContext());
468 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
469 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
470 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
471 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
473 if ( tempText
[0].equals(pText1
) &&
474 tempText
[1].equals(pText2
) &&
475 tempText
[2].equals(pText3
) &&
476 tempText
[3].equals(pText4
) ) {
482 Arrays
.fill(tempText
, null
);
483 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
484 CharSequence errorSeq
= getString(R
.string
.common_error
);
485 aDialog
.setTitle(errorSeq
);
486 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
487 aDialog
.setMessage(cseq
);
488 CharSequence okSeq
= getString(R
.string
.common_ok
);
489 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
492 public void onClick(DialogInterface dialog
, int which
) {
499 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
500 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
501 newPasswordEntered
= true
;
502 confirmingPinCode
= false
;
510 protected void confirmPincode(){
512 confirmingPinCode
= false
;
514 String rText1
= mText1
.getText().toString();
515 String rText2
= mText2
.getText().toString();
516 String rText3
= mText3
.getText().toString();
517 String rText4
= mText4
.getText().toString();
519 if ( tempText
[0].equals(rText1
) &&
520 tempText
[1].equals(rText2
) &&
521 tempText
[2].equals(rText3
) &&
522 tempText
[3].equals(rText4
) ) {
524 savePincodeAndExit();
528 Arrays
.fill(tempText
, null
);
529 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
530 CharSequence errorSeq
= getString(R
.string
.common_error
);
531 aDialog
.setTitle(errorSeq
);
532 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
533 aDialog
.setMessage(cseq
);
534 CharSequence okSeq
= getString(R
.string
.common_ok
);
535 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
538 public void onClick(DialogInterface dialog
, int which
) {
544 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
545 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
552 protected void pinCodeEnd(boolean state
){
553 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
556 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
557 aDialog
.setTitle(saveSeq
);
558 CharSequence cseq
= getString(R
.string
.pincode_stored
);
559 aDialog
.setMessage(cseq
);
562 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
563 aDialog
.setTitle(saveSeq
);
564 CharSequence cseq
= getString(R
.string
.pincode_removed
);
565 aDialog
.setMessage(cseq
);
568 CharSequence okSeq
= getString(R
.string
.common_ok
);
569 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
572 public void onClick(DialogInterface dialog
, int which
) {
581 protected void savePincodeAndExit(){
582 SharedPreferences
.Editor appPrefs
= PreferenceManager
583 .getDefaultSharedPreferences(getApplicationContext()).edit();
585 appPrefs
.putString("PrefPinCode1", tempText
[0]);
586 appPrefs
.putString("PrefPinCode2",tempText
[1]);
587 appPrefs
.putString("PrefPinCode3", tempText
[2]);
588 appPrefs
.putString("PrefPinCode4", tempText
[3]);
589 appPrefs
.putBoolean("set_pincode",true
);
599 protected void clearBoxes(){
605 mText1
.requestFocus();
610 public boolean onKeyDown(int keyCode
, KeyEvent event
){
611 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
613 if (activity
.equals("preferences")){
614 SharedPreferences
.Editor appPrefsE
= PreferenceManager
616 .getDefaultSharedPreferences(getApplicationContext()).edit();
618 SharedPreferences appPrefs
= PreferenceManager
619 .getDefaultSharedPreferences(getApplicationContext());
621 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
622 appPrefsE
.putBoolean("set_pincode",!state
);
631 return super.onKeyDown(keyCode
, event
);