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 com
.owncloud
.android
.ui
.activity
;
20 import java
.util
.Arrays
;
22 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
24 import com
.owncloud
.android
.R
;
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
.Button
;
40 import android
.widget
.EditText
;
41 import android
.widget
.TextView
;
43 public class PinCodeActivity
extends SherlockFragmentActivity
{
46 public final static String EXTRA_ACTIVITY
= "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
47 public final static String EXTRA_NEW_STATE
= "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
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 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
);
85 SharedPreferences appPrefs
= PreferenceManager
86 .getDefaultSharedPreferences(getApplicationContext());
89 // Not PIN Code defined yet.
90 // In a previous version settings is allow from start
91 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
92 setChangePincodeView(true
);
93 pinCodeChecked
= true
;
94 newPasswordEntered
= true
;
98 if (appPrefs
.getBoolean("set_pincode", false
)){
100 if (activity
.equals("preferences")){
101 // PIN has been activated yet
102 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
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 setChangePincodeView(false
);
115 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
116 pinCodeChecked
= false
;
117 setChangePincodeView(true
);
128 protected void setInitVars(){
129 confirmingPinCode
= false
;
130 pinCodeChecked
= false
;
131 newPasswordEntered
= false
;
135 protected void setInitView(){
136 bCancel
.setVisibility(View
.INVISIBLE
);
137 bCancel
.setVisibility(View
.GONE
);
138 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
142 protected void setChangePincodeView(boolean state
){
145 bCancel
.setVisibility(View
.VISIBLE
);
146 bCancel
.setOnClickListener(new OnClickListener() {
148 public void onClick(View v
) {
150 SharedPreferences
.Editor appPrefsE
= PreferenceManager
151 .getDefaultSharedPreferences(getApplicationContext()).edit();
153 SharedPreferences appPrefs
= PreferenceManager
154 .getDefaultSharedPreferences(getApplicationContext());
156 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
157 appPrefsE
.putBoolean("set_pincode",!state
);
172 protected void setTextListeners(){
174 /*------------------------------------------------
176 -------------------------------------------------*/
178 mText1
.addTextChangedListener(new TextWatcher() {
181 public void onTextChanged(CharSequence s
, int start
, int before
,
186 public void beforeTextChanged(CharSequence s
, int start
, int count
,
191 public void afterTextChanged(Editable s
) {
192 if (s
.length() > 0) {
193 if (!confirmingPinCode
){
194 tempText
[0] = mText1
.getText().toString();
197 mText2
.requestFocus();
204 /*------------------------------------------------
206 -------------------------------------------------*/
207 mText2
.addTextChangedListener(new TextWatcher() {
210 public void onTextChanged(CharSequence s
, int start
, int before
,
215 public void beforeTextChanged(CharSequence s
, int start
, int count
,
220 public void afterTextChanged(Editable s
) {
221 if (s
.length() > 0) {
222 if (!confirmingPinCode
){
223 tempText
[1] = mText2
.getText().toString();
226 mText3
.requestFocus();
231 mText2
.setOnKeyListener(new OnKeyListener() {
234 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
235 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
238 mText1
.requestFocus();
239 if (!confirmingPinCode
)
251 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
254 public void onFocusChange(View v
, boolean hasFocus
) {
255 mText2
.setCursorVisible(true
);
256 if (mText1
.getText().toString().equals("")){
257 mText2
.setSelected(false
);
258 mText2
.setCursorVisible(false
);
259 mText1
.requestFocus();
260 mText1
.setSelected(true
);
261 mText1
.setSelection(0);
268 /*------------------------------------------------
270 -------------------------------------------------*/
271 mText3
.addTextChangedListener(new TextWatcher() {
274 public void onTextChanged(CharSequence s
, int start
, int before
,
279 public void beforeTextChanged(CharSequence s
, int start
, int count
,
284 public void afterTextChanged(Editable s
) {
285 if (s
.length() > 0) {
286 if (!confirmingPinCode
){
287 tempText
[2] = mText3
.getText().toString();
289 mText4
.requestFocus();
294 mText3
.setOnKeyListener(new OnKeyListener() {
297 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
298 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
299 mText2
.requestFocus();
300 if (!confirmingPinCode
)
313 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
316 public void onFocusChange(View v
, boolean hasFocus
) {
317 mText3
.setCursorVisible(true
);
318 if (mText1
.getText().toString().equals("")){
319 mText3
.setSelected(false
);
320 mText3
.setCursorVisible(false
);
321 mText1
.requestFocus();
322 mText1
.setSelected(true
);
323 mText1
.setSelection(0);
324 }else if (mText2
.getText().toString().equals("")){
325 mText3
.setSelected(false
);
326 mText3
.setCursorVisible(false
);
327 mText2
.requestFocus();
328 mText2
.setSelected(true
);
329 mText2
.setSelection(0);
335 /*------------------------------------------------
337 -------------------------------------------------*/
338 mText4
.addTextChangedListener(new TextWatcher() {
341 public void onTextChanged(CharSequence s
, int start
, int before
,
346 public void beforeTextChanged(CharSequence s
, int start
, int count
,
351 public void afterTextChanged(Editable s
) {
352 if (s
.length() > 0) {
354 if (!confirmingPinCode
){
355 tempText
[3] = mText4
.getText().toString();
357 mText1
.requestFocus();
359 if (!pinCodeChecked
){
360 pinCodeChecked
= checkPincode();
363 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
365 } else if (pinCodeChecked
){
367 Intent intent
= getIntent();
368 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
370 if (newState
.equals("false")){
371 SharedPreferences
.Editor appPrefs
= PreferenceManager
372 .getDefaultSharedPreferences(getApplicationContext()).edit();
373 appPrefs
.putBoolean("set_pincode",false
);
381 if (!confirmingPinCode
){
382 pinCodeChangeRequest();
397 mText4
.setOnKeyListener(new OnKeyListener() {
400 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
401 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
402 mText3
.requestFocus();
403 if (!confirmingPinCode
)
415 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
418 public void onFocusChange(View v
, boolean hasFocus
) {
419 mText4
.setCursorVisible(true
);
421 if (mText1
.getText().toString().equals("")){
422 mText4
.setSelected(false
);
423 mText4
.setCursorVisible(false
);
424 mText1
.requestFocus();
425 mText1
.setSelected(true
);
426 mText1
.setSelection(0);
427 }else if (mText2
.getText().toString().equals("")){
428 mText4
.setSelected(false
);
429 mText4
.setCursorVisible(false
);
430 mText2
.requestFocus();
431 mText2
.setSelected(true
);
432 mText2
.setSelection(0);
433 }else if (mText3
.getText().toString().equals("")){
434 mText4
.setSelected(false
);
435 mText4
.setCursorVisible(false
);
436 mText3
.requestFocus();
437 mText3
.setSelected(true
);
438 mText3
.setSelection(0);
446 } // end setTextListener
449 protected void pinCodeChangeRequest(){
452 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
453 confirmingPinCode
=true
;
458 protected boolean checkPincode(){
461 SharedPreferences appPrefs
= PreferenceManager
462 .getDefaultSharedPreferences(getApplicationContext());
464 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
465 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
466 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
467 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
469 if ( tempText
[0].equals(pText1
) &&
470 tempText
[1].equals(pText2
) &&
471 tempText
[2].equals(pText3
) &&
472 tempText
[3].equals(pText4
) ) {
478 Arrays
.fill(tempText
, null
);
479 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
480 CharSequence errorSeq
= getString(R
.string
.common_error
);
481 aDialog
.setTitle(errorSeq
);
482 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
483 aDialog
.setMessage(cseq
);
484 CharSequence okSeq
= getString(R
.string
.common_ok
);
485 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
488 public void onClick(DialogInterface dialog
, int which
) {
495 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
496 newPasswordEntered
= true
;
497 confirmingPinCode
= false
;
505 protected void confirmPincode(){
507 confirmingPinCode
= false
;
509 String rText1
= mText1
.getText().toString();
510 String rText2
= mText2
.getText().toString();
511 String rText3
= mText3
.getText().toString();
512 String rText4
= mText4
.getText().toString();
514 if ( tempText
[0].equals(rText1
) &&
515 tempText
[1].equals(rText2
) &&
516 tempText
[2].equals(rText3
) &&
517 tempText
[3].equals(rText4
) ) {
519 savePincodeAndExit();
523 Arrays
.fill(tempText
, null
);
524 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
525 CharSequence errorSeq
= getString(R
.string
.common_error
);
526 aDialog
.setTitle(errorSeq
);
527 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
528 aDialog
.setMessage(cseq
);
529 CharSequence okSeq
= getString(R
.string
.common_ok
);
530 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
533 public void onClick(DialogInterface dialog
, int which
) {
539 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
546 protected void pinCodeEnd(boolean state
){
547 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
550 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
551 aDialog
.setTitle(saveSeq
);
552 CharSequence cseq
= getString(R
.string
.pincode_stored
);
553 aDialog
.setMessage(cseq
);
556 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
557 aDialog
.setTitle(saveSeq
);
558 CharSequence cseq
= getString(R
.string
.pincode_removed
);
559 aDialog
.setMessage(cseq
);
562 CharSequence okSeq
= getString(R
.string
.common_ok
);
563 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
566 public void onClick(DialogInterface dialog
, int which
) {
575 protected void savePincodeAndExit(){
576 SharedPreferences
.Editor appPrefs
= PreferenceManager
577 .getDefaultSharedPreferences(getApplicationContext()).edit();
579 appPrefs
.putString("PrefPinCode1", tempText
[0]);
580 appPrefs
.putString("PrefPinCode2",tempText
[1]);
581 appPrefs
.putString("PrefPinCode3", tempText
[2]);
582 appPrefs
.putString("PrefPinCode4", tempText
[3]);
583 appPrefs
.putBoolean("set_pincode",true
);
593 protected void clearBoxes(){
599 mText1
.requestFocus();
604 public boolean onKeyDown(int keyCode
, KeyEvent event
){
605 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
607 if (activity
.equals("preferences")){
608 SharedPreferences
.Editor appPrefsE
= PreferenceManager
610 .getDefaultSharedPreferences(getApplicationContext()).edit();
612 SharedPreferences appPrefs
= PreferenceManager
613 .getDefaultSharedPreferences(getApplicationContext());
615 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
616 appPrefsE
.putBoolean("set_pincode",!state
);
625 return super.onKeyDown(keyCode
, event
);