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
;
24 import android
.app
.AlertDialog
;
25 import android
.content
.DialogInterface
;
26 import android
.content
.Intent
;
27 import android
.content
.SharedPreferences
;
28 import android
.os
.Bundle
;
29 import android
.preference
.PreferenceManager
;
30 import android
.text
.Editable
;
31 import android
.text
.TextWatcher
;
32 import android
.view
.KeyEvent
;
33 import android
.view
.View
;
34 import android
.view
.View
.OnClickListener
;
35 import android
.view
.View
.OnFocusChangeListener
;
36 import android
.view
.View
.OnKeyListener
;
37 import android
.widget
.Button
;
38 import android
.widget
.EditText
;
39 import android
.widget
.TextView
;
41 public class PinCodeActivity
extends SherlockFragmentActivity
{
44 public final static String EXTRA_ACTIVITY
= "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
45 public final static String EXTRA_NEW_STATE
= "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
49 TextView mPinHdrExplanation
;
55 String
[] tempText
={"","","",""};
59 boolean confirmingPinCode
= false
;
60 boolean pinCodeChecked
= false
;
61 boolean newPasswordEntered
= false
;
62 boolean bChange
= true
; // to control that only one blocks jump
63 int tCounter
; // Count the number of attempts an user could introduce the PIN code
66 protected void onCreate(Bundle savedInstanceState
) {
67 super.onCreate(savedInstanceState
);
68 setContentView(R
.layout
.pincodelock
);
70 Intent intent
= getIntent();
71 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
73 bCancel
= (Button
) findViewById(R
.id
.cancel
);
74 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
75 mPinHdrExplanation
= (TextView
) findViewById(R
.id
.pinHdrExpl
);
76 mText1
= (EditText
) findViewById(R
.id
.txt1
);
77 mText1
.requestFocus();
78 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
79 mText2
= (EditText
) findViewById(R
.id
.txt2
);
80 mText3
= (EditText
) findViewById(R
.id
.txt3
);
81 mText4
= (EditText
) findViewById(R
.id
.txt4
);
83 SharedPreferences appPrefs
= PreferenceManager
84 .getDefaultSharedPreferences(getApplicationContext());
87 // Not PIN Code defined yet.
88 // In a previous version settings is allow from start
89 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
90 setChangePincodeView(true
);
91 pinCodeChecked
= true
;
92 newPasswordEntered
= true
;
96 if (appPrefs
.getBoolean("set_pincode", false
)){
98 if (activity
.equals("preferences")){
99 // PIN has been activated yet
100 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
101 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
102 pinCodeChecked
= true
; // No need to check it
103 setChangePincodeView(true
);
106 bCancel
.setVisibility(View
.INVISIBLE
);
107 bCancel
.setVisibility(View
.GONE
);
108 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
109 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
110 setChangePincodeView(false
);
115 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
116 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
117 pinCodeChecked
= false
;
118 setChangePincodeView(true
);
129 protected void setInitVars(){
130 confirmingPinCode
= false
;
131 pinCodeChecked
= false
;
132 newPasswordEntered
= false
;
136 protected void setInitView(){
137 bCancel
.setVisibility(View
.INVISIBLE
);
138 bCancel
.setVisibility(View
.GONE
);
139 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
140 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
144 protected void setChangePincodeView(boolean state
){
147 bCancel
.setVisibility(View
.VISIBLE
);
148 bCancel
.setOnClickListener(new OnClickListener() {
150 public void onClick(View v
) {
152 SharedPreferences
.Editor appPrefsE
= PreferenceManager
153 .getDefaultSharedPreferences(getApplicationContext()).edit();
155 SharedPreferences appPrefs
= PreferenceManager
156 .getDefaultSharedPreferences(getApplicationContext());
158 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
159 appPrefsE
.putBoolean("set_pincode",!state
);
174 protected void setTextListeners(){
176 /*------------------------------------------------
178 -------------------------------------------------*/
180 mText1
.addTextChangedListener(new TextWatcher() {
183 public void onTextChanged(CharSequence s
, int start
, int before
,
188 public void beforeTextChanged(CharSequence s
, int start
, int count
,
193 public void afterTextChanged(Editable s
) {
194 if (s
.length() > 0) {
195 if (!confirmingPinCode
){
196 tempText
[0] = mText1
.getText().toString();
199 mText2
.requestFocus();
206 /*------------------------------------------------
208 -------------------------------------------------*/
209 mText2
.addTextChangedListener(new TextWatcher() {
212 public void onTextChanged(CharSequence s
, int start
, int before
,
217 public void beforeTextChanged(CharSequence s
, int start
, int count
,
222 public void afterTextChanged(Editable s
) {
223 if (s
.length() > 0) {
224 if (!confirmingPinCode
){
225 tempText
[1] = mText2
.getText().toString();
228 mText3
.requestFocus();
233 mText2
.setOnKeyListener(new OnKeyListener() {
236 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
237 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
240 mText1
.requestFocus();
241 if (!confirmingPinCode
)
253 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
256 public void onFocusChange(View v
, boolean hasFocus
) {
257 mText2
.setCursorVisible(true
);
258 if (mText1
.getText().toString().equals("")){
259 mText2
.setSelected(false
);
260 mText2
.setCursorVisible(false
);
261 mText1
.requestFocus();
262 mText1
.setSelected(true
);
263 mText1
.setSelection(0);
270 /*------------------------------------------------
272 -------------------------------------------------*/
273 mText3
.addTextChangedListener(new TextWatcher() {
276 public void onTextChanged(CharSequence s
, int start
, int before
,
281 public void beforeTextChanged(CharSequence s
, int start
, int count
,
286 public void afterTextChanged(Editable s
) {
287 if (s
.length() > 0) {
288 if (!confirmingPinCode
){
289 tempText
[2] = mText3
.getText().toString();
291 mText4
.requestFocus();
296 mText3
.setOnKeyListener(new OnKeyListener() {
299 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
300 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
301 mText2
.requestFocus();
302 if (!confirmingPinCode
)
315 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
318 public void onFocusChange(View v
, boolean hasFocus
) {
319 mText3
.setCursorVisible(true
);
320 if (mText1
.getText().toString().equals("")){
321 mText3
.setSelected(false
);
322 mText3
.setCursorVisible(false
);
323 mText1
.requestFocus();
324 mText1
.setSelected(true
);
325 mText1
.setSelection(0);
326 }else if (mText2
.getText().toString().equals("")){
327 mText3
.setSelected(false
);
328 mText3
.setCursorVisible(false
);
329 mText2
.requestFocus();
330 mText2
.setSelected(true
);
331 mText2
.setSelection(0);
337 /*------------------------------------------------
339 -------------------------------------------------*/
340 mText4
.addTextChangedListener(new TextWatcher() {
343 public void onTextChanged(CharSequence s
, int start
, int before
,
348 public void beforeTextChanged(CharSequence s
, int start
, int count
,
353 public void afterTextChanged(Editable s
) {
354 if (s
.length() > 0) {
356 if (!confirmingPinCode
){
357 tempText
[3] = mText4
.getText().toString();
359 mText1
.requestFocus();
361 if (!pinCodeChecked
){
362 pinCodeChecked
= checkPincode();
365 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
367 } else if (pinCodeChecked
){
369 Intent intent
= getIntent();
370 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
372 if (newState
.equals("false")){
373 SharedPreferences
.Editor appPrefs
= PreferenceManager
374 .getDefaultSharedPreferences(getApplicationContext()).edit();
375 appPrefs
.putBoolean("set_pincode",false
);
383 if (!confirmingPinCode
){
384 pinCodeChangeRequest();
399 mText4
.setOnKeyListener(new OnKeyListener() {
402 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
403 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
404 mText3
.requestFocus();
405 if (!confirmingPinCode
)
417 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
420 public void onFocusChange(View v
, boolean hasFocus
) {
421 mText4
.setCursorVisible(true
);
423 if (mText1
.getText().toString().equals("")){
424 mText4
.setSelected(false
);
425 mText4
.setCursorVisible(false
);
426 mText1
.requestFocus();
427 mText1
.setSelected(true
);
428 mText1
.setSelection(0);
429 }else if (mText2
.getText().toString().equals("")){
430 mText4
.setSelected(false
);
431 mText4
.setCursorVisible(false
);
432 mText2
.requestFocus();
433 mText2
.setSelected(true
);
434 mText2
.setSelection(0);
435 }else if (mText3
.getText().toString().equals("")){
436 mText4
.setSelected(false
);
437 mText4
.setCursorVisible(false
);
438 mText3
.requestFocus();
439 mText3
.setSelected(true
);
440 mText3
.setSelection(0);
448 } // end setTextListener
451 protected void pinCodeChangeRequest(){
454 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
455 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
456 confirmingPinCode
=true
;
461 protected boolean checkPincode(){
464 SharedPreferences appPrefs
= PreferenceManager
465 .getDefaultSharedPreferences(getApplicationContext());
467 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
468 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
469 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
470 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
472 if ( tempText
[0].equals(pText1
) &&
473 tempText
[1].equals(pText2
) &&
474 tempText
[2].equals(pText3
) &&
475 tempText
[3].equals(pText4
) ) {
481 Arrays
.fill(tempText
, null
);
482 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
483 CharSequence errorSeq
= getString(R
.string
.common_error
);
484 aDialog
.setTitle(errorSeq
);
485 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
486 aDialog
.setMessage(cseq
);
487 CharSequence okSeq
= getString(R
.string
.common_ok
);
488 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
491 public void onClick(DialogInterface dialog
, int which
) {
498 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
499 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
500 newPasswordEntered
= true
;
501 confirmingPinCode
= false
;
509 protected void confirmPincode(){
511 confirmingPinCode
= false
;
513 String rText1
= mText1
.getText().toString();
514 String rText2
= mText2
.getText().toString();
515 String rText3
= mText3
.getText().toString();
516 String rText4
= mText4
.getText().toString();
518 if ( tempText
[0].equals(rText1
) &&
519 tempText
[1].equals(rText2
) &&
520 tempText
[2].equals(rText3
) &&
521 tempText
[3].equals(rText4
) ) {
523 savePincodeAndExit();
527 Arrays
.fill(tempText
, null
);
528 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
529 CharSequence errorSeq
= getString(R
.string
.common_error
);
530 aDialog
.setTitle(errorSeq
);
531 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
532 aDialog
.setMessage(cseq
);
533 CharSequence okSeq
= getString(R
.string
.common_ok
);
534 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
537 public void onClick(DialogInterface dialog
, int which
) {
543 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
544 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
551 protected void pinCodeEnd(boolean state
){
552 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
555 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
556 aDialog
.setTitle(saveSeq
);
557 CharSequence cseq
= getString(R
.string
.pincode_stored
);
558 aDialog
.setMessage(cseq
);
561 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
562 aDialog
.setTitle(saveSeq
);
563 CharSequence cseq
= getString(R
.string
.pincode_removed
);
564 aDialog
.setMessage(cseq
);
567 CharSequence okSeq
= getString(R
.string
.common_ok
);
568 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
571 public void onClick(DialogInterface dialog
, int which
) {
580 protected void savePincodeAndExit(){
581 SharedPreferences
.Editor appPrefs
= PreferenceManager
582 .getDefaultSharedPreferences(getApplicationContext()).edit();
584 appPrefs
.putString("PrefPinCode1", tempText
[0]);
585 appPrefs
.putString("PrefPinCode2",tempText
[1]);
586 appPrefs
.putString("PrefPinCode3", tempText
[2]);
587 appPrefs
.putString("PrefPinCode4", tempText
[3]);
588 appPrefs
.putBoolean("set_pincode",true
);
598 protected void clearBoxes(){
604 mText1
.requestFocus();
609 public boolean onKeyDown(int keyCode
, KeyEvent event
){
610 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
612 if (activity
.equals("preferences")){
613 SharedPreferences
.Editor appPrefsE
= PreferenceManager
615 .getDefaultSharedPreferences(getApplicationContext()).edit();
617 SharedPreferences appPrefs
= PreferenceManager
618 .getDefaultSharedPreferences(getApplicationContext());
620 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
621 appPrefsE
.putBoolean("set_pincode",!state
);
630 return super.onKeyDown(keyCode
, event
);