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 2 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";
51 TextView mPinHdrExplanation
;
57 String
[] tempText
={"","","",""};
61 boolean confirmingPinCode
= false
;
62 boolean pinCodeChecked
= false
;
63 boolean newPasswordEntered
= false
;
64 boolean bChange
= true
; // to control that only one blocks jump
65 int tCounter
; // Count the number of attempts an user could introduce the PIN code
68 protected void onCreate(Bundle savedInstanceState
) {
69 super.onCreate(savedInstanceState
);
70 setContentView(R
.layout
.pincodelock
);
72 Intent intent
= getIntent();
73 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
75 bCancel
= (Button
) findViewById(R
.id
.cancel
);
76 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
77 mPinHdrExplanation
= (TextView
) findViewById(R
.id
.pinHdrExpl
);
78 mText1
= (EditText
) findViewById(R
.id
.txt1
);
79 mText1
.requestFocus();
80 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
81 mText2
= (EditText
) findViewById(R
.id
.txt2
);
82 mText3
= (EditText
) findViewById(R
.id
.txt3
);
83 mText4
= (EditText
) findViewById(R
.id
.txt4
);
87 SharedPreferences appPrefs
= PreferenceManager
88 .getDefaultSharedPreferences(getApplicationContext());
91 // Not PIN Code defined yet.
92 // In a previous version settings is allow from start
93 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
94 setChangePincodeView(true
);
95 pinCodeChecked
= true
;
96 newPasswordEntered
= true
;
100 if (appPrefs
.getBoolean("set_pincode", false
)){
102 if (activity
.equals("preferences")){
103 // PIN has been activated yet
104 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
105 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
106 pinCodeChecked
= true
; // No need to check it
107 setChangePincodeView(true
);
110 bCancel
.setVisibility(View
.INVISIBLE
);
111 bCancel
.setVisibility(View
.GONE
);
112 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
113 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
114 setChangePincodeView(false
);
119 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
120 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
121 pinCodeChecked
= false
;
122 setChangePincodeView(true
);
133 protected void setInitVars(){
134 confirmingPinCode
= false
;
135 pinCodeChecked
= false
;
136 newPasswordEntered
= false
;
140 protected void setInitView(){
141 bCancel
.setVisibility(View
.INVISIBLE
);
142 bCancel
.setVisibility(View
.GONE
);
143 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
144 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
148 protected void setChangePincodeView(boolean state
){
151 bCancel
.setVisibility(View
.VISIBLE
);
152 bCancel
.setOnClickListener(new OnClickListener() {
154 public void onClick(View v
) {
156 SharedPreferences
.Editor appPrefsE
= PreferenceManager
157 .getDefaultSharedPreferences(getApplicationContext()).edit();
159 SharedPreferences appPrefs
= PreferenceManager
160 .getDefaultSharedPreferences(getApplicationContext());
162 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
163 appPrefsE
.putBoolean("set_pincode",!state
);
178 protected void setTextListeners(){
180 /*------------------------------------------------
182 -------------------------------------------------*/
184 mText1
.addTextChangedListener(new TextWatcher() {
187 public void onTextChanged(CharSequence s
, int start
, int before
,
192 public void beforeTextChanged(CharSequence s
, int start
, int count
,
197 public void afterTextChanged(Editable s
) {
198 if (s
.length() > 0) {
199 if (!confirmingPinCode
){
200 tempText
[0] = mText1
.getText().toString();
203 mText2
.requestFocus();
210 /*------------------------------------------------
212 -------------------------------------------------*/
213 mText2
.addTextChangedListener(new TextWatcher() {
216 public void onTextChanged(CharSequence s
, int start
, int before
,
221 public void beforeTextChanged(CharSequence s
, int start
, int count
,
226 public void afterTextChanged(Editable s
) {
227 if (s
.length() > 0) {
228 if (!confirmingPinCode
){
229 tempText
[1] = mText2
.getText().toString();
232 mText3
.requestFocus();
237 mText2
.setOnKeyListener(new OnKeyListener() {
240 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
241 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
244 mText1
.requestFocus();
245 if (!confirmingPinCode
)
257 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
260 public void onFocusChange(View v
, boolean hasFocus
) {
261 mText2
.setCursorVisible(true
);
262 if (mText1
.getText().toString().equals("")){
263 mText2
.setSelected(false
);
264 mText2
.setCursorVisible(false
);
265 mText1
.requestFocus();
266 mText1
.setSelected(true
);
267 mText1
.setSelection(0);
274 /*------------------------------------------------
276 -------------------------------------------------*/
277 mText3
.addTextChangedListener(new TextWatcher() {
280 public void onTextChanged(CharSequence s
, int start
, int before
,
285 public void beforeTextChanged(CharSequence s
, int start
, int count
,
290 public void afterTextChanged(Editable s
) {
291 if (s
.length() > 0) {
292 if (!confirmingPinCode
){
293 tempText
[2] = mText3
.getText().toString();
295 mText4
.requestFocus();
300 mText3
.setOnKeyListener(new OnKeyListener() {
303 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
304 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
305 mText2
.requestFocus();
306 if (!confirmingPinCode
)
319 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
322 public void onFocusChange(View v
, boolean hasFocus
) {
323 mText3
.setCursorVisible(true
);
324 if (mText1
.getText().toString().equals("")){
325 mText3
.setSelected(false
);
326 mText3
.setCursorVisible(false
);
327 mText1
.requestFocus();
328 mText1
.setSelected(true
);
329 mText1
.setSelection(0);
330 }else if (mText2
.getText().toString().equals("")){
331 mText3
.setSelected(false
);
332 mText3
.setCursorVisible(false
);
333 mText2
.requestFocus();
334 mText2
.setSelected(true
);
335 mText2
.setSelection(0);
341 /*------------------------------------------------
343 -------------------------------------------------*/
344 mText4
.addTextChangedListener(new TextWatcher() {
347 public void onTextChanged(CharSequence s
, int start
, int before
,
352 public void beforeTextChanged(CharSequence s
, int start
, int count
,
357 public void afterTextChanged(Editable s
) {
358 if (s
.length() > 0) {
360 if (!confirmingPinCode
){
361 tempText
[3] = mText4
.getText().toString();
363 mText1
.requestFocus();
365 if (!pinCodeChecked
){
366 pinCodeChecked
= checkPincode();
369 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
371 } else if (pinCodeChecked
){
373 Intent intent
= getIntent();
374 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
376 if (newState
.equals("false")){
377 SharedPreferences
.Editor appPrefs
= PreferenceManager
378 .getDefaultSharedPreferences(getApplicationContext()).edit();
379 appPrefs
.putBoolean("set_pincode",false
);
387 if (!confirmingPinCode
){
388 pinCodeChangeRequest();
403 mText4
.setOnKeyListener(new OnKeyListener() {
406 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
407 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
408 mText3
.requestFocus();
409 if (!confirmingPinCode
)
421 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
424 public void onFocusChange(View v
, boolean hasFocus
) {
425 mText4
.setCursorVisible(true
);
427 if (mText1
.getText().toString().equals("")){
428 mText4
.setSelected(false
);
429 mText4
.setCursorVisible(false
);
430 mText1
.requestFocus();
431 mText1
.setSelected(true
);
432 mText1
.setSelection(0);
433 }else if (mText2
.getText().toString().equals("")){
434 mText4
.setSelected(false
);
435 mText4
.setCursorVisible(false
);
436 mText2
.requestFocus();
437 mText2
.setSelected(true
);
438 mText2
.setSelection(0);
439 }else if (mText3
.getText().toString().equals("")){
440 mText4
.setSelected(false
);
441 mText4
.setCursorVisible(false
);
442 mText3
.requestFocus();
443 mText3
.setSelected(true
);
444 mText3
.setSelection(0);
452 } // end setTextListener
455 protected void pinCodeChangeRequest(){
458 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
459 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
460 confirmingPinCode
=true
;
465 protected boolean checkPincode(){
468 SharedPreferences appPrefs
= PreferenceManager
469 .getDefaultSharedPreferences(getApplicationContext());
471 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
472 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
473 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
474 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
476 if ( tempText
[0].equals(pText1
) &&
477 tempText
[1].equals(pText2
) &&
478 tempText
[2].equals(pText3
) &&
479 tempText
[3].equals(pText4
) ) {
485 Arrays
.fill(tempText
, null
);
486 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
487 CharSequence errorSeq
= getString(R
.string
.common_error
);
488 aDialog
.setTitle(errorSeq
);
489 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
490 aDialog
.setMessage(cseq
);
491 CharSequence okSeq
= getString(R
.string
.common_ok
);
492 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
495 public void onClick(DialogInterface dialog
, int which
) {
502 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
503 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
504 newPasswordEntered
= true
;
505 confirmingPinCode
= false
;
513 protected void confirmPincode(){
515 confirmingPinCode
= false
;
517 String rText1
= mText1
.getText().toString();
518 String rText2
= mText2
.getText().toString();
519 String rText3
= mText3
.getText().toString();
520 String rText4
= mText4
.getText().toString();
522 if ( tempText
[0].equals(rText1
) &&
523 tempText
[1].equals(rText2
) &&
524 tempText
[2].equals(rText3
) &&
525 tempText
[3].equals(rText4
) ) {
527 savePincodeAndExit();
531 Arrays
.fill(tempText
, null
);
532 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
533 CharSequence errorSeq
= getString(R
.string
.common_error
);
534 aDialog
.setTitle(errorSeq
);
535 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
536 aDialog
.setMessage(cseq
);
537 CharSequence okSeq
= getString(R
.string
.common_ok
);
538 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
541 public void onClick(DialogInterface dialog
, int which
) {
547 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
548 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
555 protected void pinCodeEnd(boolean state
){
556 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
559 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
560 aDialog
.setTitle(saveSeq
);
561 CharSequence cseq
= getString(R
.string
.pincode_stored
);
562 aDialog
.setMessage(cseq
);
565 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
566 aDialog
.setTitle(saveSeq
);
567 CharSequence cseq
= getString(R
.string
.pincode_removed
);
568 aDialog
.setMessage(cseq
);
571 CharSequence okSeq
= getString(R
.string
.common_ok
);
572 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
575 public void onClick(DialogInterface dialog
, int which
) {
584 protected void savePincodeAndExit(){
585 SharedPreferences
.Editor appPrefs
= PreferenceManager
586 .getDefaultSharedPreferences(getApplicationContext()).edit();
588 appPrefs
.putString("PrefPinCode1", tempText
[0]);
589 appPrefs
.putString("PrefPinCode2",tempText
[1]);
590 appPrefs
.putString("PrefPinCode3", tempText
[2]);
591 appPrefs
.putString("PrefPinCode4", tempText
[3]);
592 appPrefs
.putBoolean("set_pincode",true
);
602 protected void clearBoxes(){
608 mText1
.requestFocus();
613 public boolean onKeyDown(int keyCode
, KeyEvent event
){
614 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
616 if (activity
.equals("preferences")){
617 SharedPreferences
.Editor appPrefsE
= PreferenceManager
619 .getDefaultSharedPreferences(getApplicationContext()).edit();
621 SharedPreferences appPrefs
= PreferenceManager
622 .getDefaultSharedPreferences(getApplicationContext());
624 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
625 appPrefsE
.putBoolean("set_pincode",!state
);
634 return super.onKeyDown(keyCode
, event
);