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 eu
.alefzero
.owncloud
.ui
.activity
;
20 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
22 import eu
.alefzero
.owncloud
.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
;
42 public class PinCodeActivity
extends SherlockFragmentActivity
{
45 public final static String EXTRA_ACTIVITY
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
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 mText1
= (EditText
) findViewById(R
.id
.txt1
);
76 mText1
.requestFocus();
77 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
78 mText2
= (EditText
) findViewById(R
.id
.txt2
);
79 mText3
= (EditText
) findViewById(R
.id
.txt3
);
80 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
) ){
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 pinCodeChecked
= true
; // No need to check it
102 setChangePincodeView(true
);
105 bCancel
.setVisibility(View
.INVISIBLE
);
106 bCancel
.setVisibility(View
.GONE
);
107 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
108 setChangePincodeView(false
);
113 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
114 pinCodeChecked
= false
;
115 setChangePincodeView(true
);
124 protected void setInitVars(){
125 confirmingPinCode
= false
;
126 pinCodeChecked
= false
;
127 newPasswordEntered
= false
;
131 protected void setInitView(){
132 bCancel
.setVisibility(View
.INVISIBLE
);
133 bCancel
.setVisibility(View
.GONE
);
134 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
138 protected void setChangePincodeView(boolean state
){
141 bCancel
.setVisibility(View
.VISIBLE
);
142 bCancel
.setOnClickListener(new OnClickListener() {
144 public void onClick(View v
) {
146 SharedPreferences
.Editor appPrefsE
= PreferenceManager
147 .getDefaultSharedPreferences(getApplicationContext()).edit();
149 SharedPreferences appPrefs
= PreferenceManager
150 .getDefaultSharedPreferences(getApplicationContext());
152 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
153 appPrefsE
.putBoolean("set_pincode",!state
);
168 protected void setTextListeners(){
170 /*------------------------------------------------
172 -------------------------------------------------*/
174 mText1
.addTextChangedListener(new TextWatcher() {
177 public void onTextChanged(CharSequence s
, int start
, int before
,
179 // TODO Auto-generated method stub
180 if (s
.length() > 0) {
181 if (!confirmingPinCode
){
182 tempText
[0] = mText1
.getText().toString();
185 mText2
.requestFocus();
190 public void beforeTextChanged(CharSequence s
, int start
, int count
,
192 // TODO Auto-generated method stub
197 public void afterTextChanged(Editable s
) {
198 // TODO Auto-generated method stub
205 /*------------------------------------------------
207 -------------------------------------------------*/
208 mText2
.addTextChangedListener(new TextWatcher() {
211 public void onTextChanged(CharSequence s
, int start
, int before
,
213 // TODO Auto-generated method stub
214 if (s
.length() > 0) {
215 if (!confirmingPinCode
){
216 tempText
[1] = mText2
.getText().toString();
218 mText3
.requestFocus();
223 public void beforeTextChanged(CharSequence s
, int start
, int count
,
225 // TODO Auto-generated method stub
230 public void afterTextChanged(Editable s
) {
231 // TODO Auto-generated method stub
236 mText2
.setOnKeyListener(new OnKeyListener() {
239 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
240 // TODO Auto-generated method stub
242 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
245 mText1
.requestFocus();
257 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
260 public void onFocusChange(View v
, boolean hasFocus
) {
261 // TODO Auto-generated method stub
262 if (mText1
.getText().toString().equals("")){
263 mText1
.requestFocus();
270 /*------------------------------------------------
272 -------------------------------------------------*/
273 mText3
.addTextChangedListener(new TextWatcher() {
276 public void onTextChanged(CharSequence s
, int start
, int before
,
278 // TODO Auto-generated method stub
279 if (s
.length() > 0) {
280 if (!confirmingPinCode
){
281 tempText
[2] = mText3
.getText().toString();
283 mText4
.requestFocus();
288 public void beforeTextChanged(CharSequence s
, int start
, int count
,
290 // TODO Auto-generated method stub
295 public void afterTextChanged(Editable s
) {
296 // TODO Auto-generated method stub
301 mText3
.setOnKeyListener(new OnKeyListener() {
304 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
305 // TODO Auto-generated method stub
307 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
308 mText2
.requestFocus();
321 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
324 public void onFocusChange(View v
, boolean hasFocus
) {
325 // TODO Auto-generated method stub
326 if (mText1
.getText().toString().equals("")){
327 mText1
.requestFocus();
328 }else if (mText2
.getText().toString().equals("")){
329 mText2
.requestFocus();
335 /*------------------------------------------------
337 -------------------------------------------------*/
338 mText4
.addTextChangedListener(new TextWatcher() {
341 public void onTextChanged(CharSequence s
, int start
, int before
,
344 if (s
.length() > 0) {
346 if (!confirmingPinCode
){
347 tempText
[3] = mText4
.getText().toString();
349 mText1
.requestFocus();
351 if (!pinCodeChecked
){
352 pinCodeChecked
= checkPincode();
355 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
357 } else if (pinCodeChecked
){
359 Intent intent
= getIntent();
360 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
362 if (newState
.equals("false")){
363 SharedPreferences
.Editor appPrefs
= PreferenceManager
364 .getDefaultSharedPreferences(getApplicationContext()).edit();
365 appPrefs
.putBoolean("set_pincode",false
);
373 if (!confirmingPinCode
&& !newPasswordEntered
){
374 pinCodeChangeRequest();
375 } else if (newPasswordEntered
&& !confirmingPinCode
){
376 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
377 confirmingPinCode
= true
;
391 public void beforeTextChanged(CharSequence s
, int start
, int count
,
393 // TODO Auto-generated method stub
398 public void afterTextChanged(Editable s
) {
399 // TODO Auto-generated method stub
406 mText4
.setOnKeyListener(new OnKeyListener() {
409 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
410 // TODO Auto-generated method stub
412 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
413 mText3
.requestFocus();
425 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
428 public void onFocusChange(View v
, boolean hasFocus
) {
429 // TODO Auto-generated method stub
430 if (mText1
.getText().toString().equals("")){
431 mText1
.requestFocus();
432 }else if (mText2
.getText().toString().equals("")){
433 mText2
.requestFocus();
434 }else if (mText3
.getText().toString().equals("")){
435 mText3
.requestFocus();
443 } // end setTextListener
446 protected void pinCodeChangeRequest(){
449 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
450 confirmingPinCode
=true
;
455 protected boolean checkPincode(){
458 SharedPreferences appPrefs
= PreferenceManager
459 .getDefaultSharedPreferences(getApplicationContext());
461 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
462 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
463 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
464 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
466 if ( tempText
[0].equals(pText1
) &&
467 tempText
[1].equals(pText2
) &&
468 tempText
[2].equals(pText3
) &&
469 tempText
[3].equals(pText4
) ) {
475 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
476 aDialog
.setTitle("ERROR");
477 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
478 aDialog
.setMessage(cseq
);
479 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
482 public void onClick(DialogInterface dialog
, int which
) {
483 // TODO Auto-generated method stub("");
490 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
491 newPasswordEntered
= true
;
492 confirmingPinCode
= false
;
500 protected void confirmPincode(){
502 confirmingPinCode
= false
;
504 String rText1
= mText1
.getText().toString();
505 String rText2
= mText2
.getText().toString();
506 String rText3
= mText3
.getText().toString();
507 String rText4
= mText4
.getText().toString();
509 if ( tempText
[0].equals(rText1
) &&
510 tempText
[1].equals(rText2
) &&
511 tempText
[2].equals(rText3
) &&
512 tempText
[3].equals(rText4
) ) {
514 savePincodeAndExit();
518 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
519 aDialog
.setTitle("ERROR");
520 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
521 aDialog
.setMessage(cseq
);
522 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
525 public void onClick(DialogInterface dialog
, int which
) {
526 // TODO Auto-generated method stub("");
532 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
539 protected void pinCodeEnd(boolean state
){
540 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
543 aDialog
.setTitle("SAVE & EXIT");
544 aDialog
.setMessage("PIN Code Activated");
546 aDialog
.setTitle("SAVE & EXIT");
547 aDialog
.setMessage("PIN Code Removed");
550 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
553 public void onClick(DialogInterface dialog
, int which
) {
554 // TODO Auto-generated method stub("");
563 protected void savePincodeAndExit(){
564 SharedPreferences
.Editor appPrefs
= PreferenceManager
565 .getDefaultSharedPreferences(getApplicationContext()).edit();
567 appPrefs
.putString("PrefPinCode1", tempText
[0]);
568 appPrefs
.putString("PrefPinCode2",tempText
[1]);
569 appPrefs
.putString("PrefPinCode3", tempText
[2]);
570 appPrefs
.putString("PrefPinCode4", tempText
[3]);
571 appPrefs
.putBoolean("set_pincode",true
);
581 protected void clearBoxes(){
587 mText1
.requestFocus();
592 public boolean onKeyDown(int keyCode
, KeyEvent event
){
593 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
595 if (activity
.equals("preferences")){
596 SharedPreferences
.Editor appPrefsE
= PreferenceManager
598 .getDefaultSharedPreferences(getApplicationContext()).edit();
600 SharedPreferences appPrefs
= PreferenceManager
601 .getDefaultSharedPreferences(getApplicationContext());
603 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
604 appPrefsE
.putBoolean("set_pincode",!state
);
613 return super.onKeyDown(keyCode
, event
);