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
.graphics
.Typeface
;
31 import android
.os
.Bundle
;
32 import android
.os
.Handler
;
33 import android
.preference
.PreferenceManager
;
34 import android
.text
.Editable
;
35 import android
.text
.InputType
;
36 import android
.text
.TextWatcher
;
37 import android
.text
.method
.PasswordTransformationMethod
;
38 import android
.view
.KeyEvent
;
39 import android
.view
.View
;
40 import android
.view
.View
.OnClickListener
;
41 import android
.view
.View
.OnFocusChangeListener
;
42 import android
.view
.View
.OnKeyListener
;
43 import android
.view
.ViewGroup
;
44 import android
.widget
.Button
;
45 import android
.widget
.EditText
;
46 import android
.widget
.TextView
;
49 public class PinCodeActivity
extends SherlockFragmentActivity
{
52 public final static String EXTRA_ACTIVITY
= "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
53 public final static String EXTRA_NEW_STATE
= "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
62 String
[] tempText
={"","","",""};
66 boolean confirmingPinCode
= false
;
67 boolean pinCodeChecked
= false
;
68 boolean newPasswordEntered
= false
;
69 boolean bChange
= true
; // to control that only one blocks jump
70 int tCounter
; // Count the number of attempts an user could introduce the PIN code
73 protected void onCreate(Bundle savedInstanceState
) {
74 super.onCreate(savedInstanceState
);
75 setContentView(R
.layout
.pincodelock
);
77 Intent intent
= getIntent();
78 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
80 bCancel
= (Button
) findViewById(R
.id
.cancel
);
81 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
82 mText1
= (EditText
) findViewById(R
.id
.txt1
);
83 mText1
.requestFocus();
84 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
85 mText2
= (EditText
) findViewById(R
.id
.txt2
);
86 mText3
= (EditText
) findViewById(R
.id
.txt3
);
87 mText4
= (EditText
) findViewById(R
.id
.txt4
);
91 SharedPreferences appPrefs
= PreferenceManager
92 .getDefaultSharedPreferences(getApplicationContext());
95 // Not PIN Code defined yet.
96 // In a previous version settings is allow from start
97 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
98 setChangePincodeView(true
);
99 pinCodeChecked
= true
;
100 newPasswordEntered
= true
;
104 if (appPrefs
.getBoolean("set_pincode", false
)){
106 if (activity
.equals("preferences")){
107 // PIN has been activated yet
108 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
109 pinCodeChecked
= true
; // No need to check it
110 setChangePincodeView(true
);
113 bCancel
.setVisibility(View
.INVISIBLE
);
114 bCancel
.setVisibility(View
.GONE
);
115 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
116 setChangePincodeView(false
);
121 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
122 pinCodeChecked
= false
;
123 setChangePincodeView(true
);
134 protected void setInitVars(){
135 confirmingPinCode
= false
;
136 pinCodeChecked
= false
;
137 newPasswordEntered
= false
;
141 protected void setInitView(){
142 bCancel
.setVisibility(View
.INVISIBLE
);
143 bCancel
.setVisibility(View
.GONE
);
144 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
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 // TODO Auto-generated method stub
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 // TODO Auto-generated method stub
265 mText2
.setCursorVisible(true
);
266 if (mText1
.getText().toString().equals("")){
267 mText2
.setSelected(false
);
268 mText2
.setCursorVisible(false
);
269 mText1
.requestFocus();
270 mText1
.setSelected(true
);
271 mText1
.setSelection(0);
278 /*------------------------------------------------
280 -------------------------------------------------*/
281 mText3
.addTextChangedListener(new TextWatcher() {
284 public void onTextChanged(CharSequence s
, int start
, int before
,
289 public void beforeTextChanged(CharSequence s
, int start
, int count
,
294 public void afterTextChanged(Editable s
) {
295 if (s
.length() > 0) {
296 if (!confirmingPinCode
){
297 tempText
[2] = mText3
.getText().toString();
299 mText4
.requestFocus();
304 mText3
.setOnKeyListener(new OnKeyListener() {
307 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
308 // TODO Auto-generated method stub
310 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
311 mText2
.requestFocus();
312 if (!confirmingPinCode
)
325 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
328 public void onFocusChange(View v
, boolean hasFocus
) {
329 // TODO Auto-generated method stub
330 mText3
.setCursorVisible(true
);
331 if (mText1
.getText().toString().equals("")){
332 mText3
.setSelected(false
);
333 mText3
.setCursorVisible(false
);
334 mText1
.requestFocus();
335 mText1
.setSelected(true
);
336 mText1
.setSelection(0);
337 }else if (mText2
.getText().toString().equals("")){
338 mText3
.setSelected(false
);
339 mText3
.setCursorVisible(false
);
340 mText2
.requestFocus();
341 mText2
.setSelected(true
);
342 mText2
.setSelection(0);
348 /*------------------------------------------------
350 -------------------------------------------------*/
351 mText4
.addTextChangedListener(new TextWatcher() {
354 public void onTextChanged(CharSequence s
, int start
, int before
,
359 public void beforeTextChanged(CharSequence s
, int start
, int count
,
364 public void afterTextChanged(Editable s
) {
365 if (s
.length() > 0) {
367 if (!confirmingPinCode
){
368 tempText
[3] = mText4
.getText().toString();
370 mText1
.requestFocus();
372 if (!pinCodeChecked
){
373 pinCodeChecked
= checkPincode();
376 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
378 } else if (pinCodeChecked
){
380 Intent intent
= getIntent();
381 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
383 if (newState
.equals("false")){
384 SharedPreferences
.Editor appPrefs
= PreferenceManager
385 .getDefaultSharedPreferences(getApplicationContext()).edit();
386 appPrefs
.putBoolean("set_pincode",false
);
394 if (!confirmingPinCode
){
395 pinCodeChangeRequest();
410 mText4
.setOnKeyListener(new OnKeyListener() {
413 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
414 // TODO Auto-generated method stub
416 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
417 mText3
.requestFocus();
418 if (!confirmingPinCode
)
430 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
433 public void onFocusChange(View v
, boolean hasFocus
) {
434 // TODO Auto-generated method stub
436 mText4
.setCursorVisible(true
);
438 if (mText1
.getText().toString().equals("")){
439 mText4
.setSelected(false
);
440 mText4
.setCursorVisible(false
);
441 mText1
.requestFocus();
442 mText1
.setSelected(true
);
443 mText1
.setSelection(0);
444 }else if (mText2
.getText().toString().equals("")){
445 mText4
.setSelected(false
);
446 mText4
.setCursorVisible(false
);
447 mText2
.requestFocus();
448 mText2
.setSelected(true
);
449 mText2
.setSelection(0);
450 }else if (mText3
.getText().toString().equals("")){
451 mText4
.setSelected(false
);
452 mText4
.setCursorVisible(false
);
453 mText3
.requestFocus();
454 mText3
.setSelected(true
);
455 mText3
.setSelection(0);
463 } // end setTextListener
466 protected void pinCodeChangeRequest(){
469 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
470 confirmingPinCode
=true
;
475 protected boolean checkPincode(){
478 SharedPreferences appPrefs
= PreferenceManager
479 .getDefaultSharedPreferences(getApplicationContext());
481 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
482 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
483 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
484 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
486 if ( tempText
[0].equals(pText1
) &&
487 tempText
[1].equals(pText2
) &&
488 tempText
[2].equals(pText3
) &&
489 tempText
[3].equals(pText4
) ) {
495 Arrays
.fill(tempText
, null
);
496 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
497 CharSequence errorSeq
= getString(R
.string
.common_error
);
498 aDialog
.setTitle(errorSeq
);
499 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
500 aDialog
.setMessage(cseq
);
501 CharSequence okSeq
= getString(R
.string
.common_ok
);
502 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
505 public void onClick(DialogInterface dialog
, int which
) {
506 // TODO Auto-generated method stub("");
513 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
514 newPasswordEntered
= true
;
515 confirmingPinCode
= false
;
523 protected void confirmPincode(){
525 confirmingPinCode
= false
;
527 String rText1
= mText1
.getText().toString();
528 String rText2
= mText2
.getText().toString();
529 String rText3
= mText3
.getText().toString();
530 String rText4
= mText4
.getText().toString();
532 if ( tempText
[0].equals(rText1
) &&
533 tempText
[1].equals(rText2
) &&
534 tempText
[2].equals(rText3
) &&
535 tempText
[3].equals(rText4
) ) {
537 savePincodeAndExit();
541 Arrays
.fill(tempText
, null
);
542 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
543 CharSequence errorSeq
= getString(R
.string
.common_error
);
544 aDialog
.setTitle(errorSeq
);
545 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
546 aDialog
.setMessage(cseq
);
547 CharSequence okSeq
= getString(R
.string
.common_ok
);
548 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
551 public void onClick(DialogInterface dialog
, int which
) {
552 // TODO Auto-generated method stub("");
558 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
565 protected void pinCodeEnd(boolean state
){
566 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
569 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
570 aDialog
.setTitle(saveSeq
);
571 CharSequence cseq
= getString(R
.string
.pincode_stored
);
572 aDialog
.setMessage(cseq
);
575 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
576 aDialog
.setTitle(saveSeq
);
577 CharSequence cseq
= getString(R
.string
.pincode_removed
);
578 aDialog
.setMessage(cseq
);
581 CharSequence okSeq
= getString(R
.string
.common_ok
);
582 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
585 public void onClick(DialogInterface dialog
, int which
) {
586 // TODO Auto-generated method stub("");
595 protected void savePincodeAndExit(){
596 SharedPreferences
.Editor appPrefs
= PreferenceManager
597 .getDefaultSharedPreferences(getApplicationContext()).edit();
599 appPrefs
.putString("PrefPinCode1", tempText
[0]);
600 appPrefs
.putString("PrefPinCode2",tempText
[1]);
601 appPrefs
.putString("PrefPinCode3", tempText
[2]);
602 appPrefs
.putString("PrefPinCode4", tempText
[3]);
603 appPrefs
.putBoolean("set_pincode",true
);
613 protected void clearBoxes(){
619 mText1
.requestFocus();
624 public boolean onKeyDown(int keyCode
, KeyEvent event
){
625 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
627 if (activity
.equals("preferences")){
628 SharedPreferences
.Editor appPrefsE
= PreferenceManager
630 .getDefaultSharedPreferences(getApplicationContext()).edit();
632 SharedPreferences appPrefs
= PreferenceManager
633 .getDefaultSharedPreferences(getApplicationContext());
635 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
636 appPrefsE
.putBoolean("set_pincode",!state
);
645 return super.onKeyDown(keyCode
, event
);