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 java
.util
.Arrays
;
22 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
24 import eu
.alefzero
.owncloud
.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
;
44 public class PinCodeActivity
extends SherlockFragmentActivity
{
47 public final static String EXTRA_ACTIVITY
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
48 public final static String EXTRA_NEW_STATE
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
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 mText1
= (EditText
) findViewById(R
.id
.txt1
);
78 mText1
.requestFocus();
79 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
80 mText2
= (EditText
) findViewById(R
.id
.txt2
);
81 mText3
= (EditText
) findViewById(R
.id
.txt3
);
82 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
) ){
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_enter_pin_code
);
116 pinCodeChecked
= false
;
117 setChangePincodeView(true
);
126 protected void setInitVars(){
127 confirmingPinCode
= false
;
128 pinCodeChecked
= false
;
129 newPasswordEntered
= false
;
133 protected void setInitView(){
134 bCancel
.setVisibility(View
.INVISIBLE
);
135 bCancel
.setVisibility(View
.GONE
);
136 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
140 protected void setChangePincodeView(boolean state
){
143 bCancel
.setVisibility(View
.VISIBLE
);
144 bCancel
.setOnClickListener(new OnClickListener() {
146 public void onClick(View v
) {
148 SharedPreferences
.Editor appPrefsE
= PreferenceManager
149 .getDefaultSharedPreferences(getApplicationContext()).edit();
151 SharedPreferences appPrefs
= PreferenceManager
152 .getDefaultSharedPreferences(getApplicationContext());
154 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
155 appPrefsE
.putBoolean("set_pincode",!state
);
170 protected void setTextListeners(){
172 /*------------------------------------------------
174 -------------------------------------------------*/
176 mText1
.addTextChangedListener(new TextWatcher() {
179 public void onTextChanged(CharSequence s
, int start
, int before
,
181 // TODO Auto-generated method stub
182 if (s
.length() > 0) {
183 if (!confirmingPinCode
){
184 tempText
[0] = mText1
.getText().toString();
188 mText2
.requestFocus();
194 public void beforeTextChanged(CharSequence s
, int start
, int count
,
196 // TODO Auto-generated method stub
201 public void afterTextChanged(Editable s
) {
202 // TODO Auto-generated method stub
209 /*------------------------------------------------
211 -------------------------------------------------*/
212 mText2
.addTextChangedListener(new TextWatcher() {
215 public void onTextChanged(CharSequence s
, int start
, int before
,
217 // TODO Auto-generated method stub
218 if (s
.length() > 0) {
219 if (!confirmingPinCode
){
220 tempText
[1] = mText2
.getText().toString();
223 mText3
.requestFocus();
229 public void beforeTextChanged(CharSequence s
, int start
, int count
,
231 // TODO Auto-generated method stub
236 public void afterTextChanged(Editable s
) {
237 // TODO Auto-generated method stub
242 mText2
.setOnKeyListener(new OnKeyListener() {
245 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
246 // TODO Auto-generated method stub
248 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
251 mText1
.requestFocus();
252 if (!confirmingPinCode
)
264 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
267 public void onFocusChange(View v
, boolean hasFocus
) {
268 // TODO Auto-generated method stub
269 if (mText1
.getText().toString().equals("")){
270 mText1
.requestFocus();
279 /*------------------------------------------------
281 -------------------------------------------------*/
282 mText3
.addTextChangedListener(new TextWatcher() {
285 public void onTextChanged(CharSequence s
, int start
, int before
,
287 // TODO Auto-generated method stub
288 if (s
.length() > 0) {
289 if (!confirmingPinCode
){
290 tempText
[2] = mText3
.getText().toString();
292 mText4
.requestFocus();
298 public void beforeTextChanged(CharSequence s
, int start
, int count
,
300 // TODO Auto-generated method stub
305 public void afterTextChanged(Editable s
) {
306 // TODO Auto-generated method stub
311 mText3
.setOnKeyListener(new OnKeyListener() {
314 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
315 // TODO Auto-generated method stub
317 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
318 mText2
.requestFocus();
319 if (!confirmingPinCode
)
332 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
335 public void onFocusChange(View v
, boolean hasFocus
) {
336 // TODO Auto-generated method stub
337 if (mText1
.getText().toString().equals("")){
338 mText1
.requestFocus();
339 }else if (mText2
.getText().toString().equals("")){
340 mText2
.requestFocus();
346 /*------------------------------------------------
348 -------------------------------------------------*/
349 mText4
.addTextChangedListener(new TextWatcher() {
352 public void onTextChanged(CharSequence s
, int start
, int before
,
355 if (s
.length() > 0) {
357 if (!confirmingPinCode
){
358 tempText
[3] = mText4
.getText().toString();
360 mText1
.requestFocus();
362 if (!pinCodeChecked
){
363 pinCodeChecked
= checkPincode();
366 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
368 } else if (pinCodeChecked
){
370 Intent intent
= getIntent();
371 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
373 if (newState
.equals("false")){
374 SharedPreferences
.Editor appPrefs
= PreferenceManager
375 .getDefaultSharedPreferences(getApplicationContext()).edit();
376 appPrefs
.putBoolean("set_pincode",false
);
384 if (!confirmingPinCode
){
385 pinCodeChangeRequest();
399 public void beforeTextChanged(CharSequence s
, int start
, int count
,
401 // TODO Auto-generated method stub
406 public void afterTextChanged(Editable s
) {
407 // TODO Auto-generated method stub
414 mText4
.setOnKeyListener(new OnKeyListener() {
417 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
418 // TODO Auto-generated method stub
420 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
421 mText3
.requestFocus();
422 if (!confirmingPinCode
)
434 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
437 public void onFocusChange(View v
, boolean hasFocus
) {
438 // TODO Auto-generated method stub
439 if (mText1
.getText().toString().equals("")){
440 mText1
.requestFocus();
441 }else if (mText2
.getText().toString().equals("")){
442 mText2
.requestFocus();
443 }else if (mText3
.getText().toString().equals("")){
444 mText3
.requestFocus();
452 } // end setTextListener
455 protected void pinCodeChangeRequest(){
458 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
459 confirmingPinCode
=true
;
464 protected boolean checkPincode(){
467 SharedPreferences appPrefs
= PreferenceManager
468 .getDefaultSharedPreferences(getApplicationContext());
470 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
471 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
472 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
473 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
475 if ( tempText
[0].equals(pText1
) &&
476 tempText
[1].equals(pText2
) &&
477 tempText
[2].equals(pText3
) &&
478 tempText
[3].equals(pText4
) ) {
484 Arrays
.fill(tempText
, null
);
485 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
486 aDialog
.setTitle("ERROR");
487 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
488 aDialog
.setMessage(cseq
);
489 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
492 public void onClick(DialogInterface dialog
, int which
) {
493 // TODO Auto-generated method stub("");
500 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
501 newPasswordEntered
= true
;
502 confirmingPinCode
= false
;
510 protected void confirmPincode(){
512 confirmingPinCode
= false
;
514 String rText1
= mText1
.getText().toString();
515 String rText2
= mText2
.getText().toString();
516 String rText3
= mText3
.getText().toString();
517 String rText4
= mText4
.getText().toString();
519 if ( tempText
[0].equals(rText1
) &&
520 tempText
[1].equals(rText2
) &&
521 tempText
[2].equals(rText3
) &&
522 tempText
[3].equals(rText4
) ) {
524 savePincodeAndExit();
528 Arrays
.fill(tempText
, null
);
529 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
530 aDialog
.setTitle("ERROR");
531 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
532 aDialog
.setMessage(cseq
);
533 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
536 public void onClick(DialogInterface dialog
, int which
) {
537 // TODO Auto-generated method stub("");
543 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
550 protected void pinCodeEnd(boolean state
){
551 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
554 aDialog
.setTitle("SAVE & EXIT");
555 aDialog
.setMessage("PIN Code Activated");
557 aDialog
.setTitle("SAVE & EXIT");
558 aDialog
.setMessage("PIN Code Removed");
561 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
564 public void onClick(DialogInterface dialog
, int which
) {
565 // TODO Auto-generated method stub("");
574 protected void savePincodeAndExit(){
575 SharedPreferences
.Editor appPrefs
= PreferenceManager
576 .getDefaultSharedPreferences(getApplicationContext()).edit();
578 appPrefs
.putString("PrefPinCode1", tempText
[0]);
579 appPrefs
.putString("PrefPinCode2",tempText
[1]);
580 appPrefs
.putString("PrefPinCode3", tempText
[2]);
581 appPrefs
.putString("PrefPinCode4", tempText
[3]);
582 appPrefs
.putBoolean("set_pincode",true
);
592 protected void clearBoxes(){
598 mText1
.requestFocus();
603 public boolean onKeyDown(int keyCode
, KeyEvent event
){
604 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
606 if (activity
.equals("preferences")){
607 SharedPreferences
.Editor appPrefsE
= PreferenceManager
609 .getDefaultSharedPreferences(getApplicationContext()).edit();
611 SharedPreferences appPrefs
= PreferenceManager
612 .getDefaultSharedPreferences(getApplicationContext());
614 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
615 appPrefsE
.putBoolean("set_pincode",!state
);
624 return super.onKeyDown(keyCode
, event
);