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 de 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
;
94 }else if (appPrefs
.getBoolean("set_pincode", false
)){
95 if (activity
.equals("preferences")){
96 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
97 setChangePincodeView(true
);
99 bCancel
.setVisibility(View
.INVISIBLE
);
100 bCancel
.setVisibility(View
.GONE
);
101 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
102 setChangePincodeView(false
);
106 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
107 pinCodeChecked
= true
;
108 setChangePincodeView(true
);
118 protected void setInitView(){
119 bCancel
.setVisibility(View
.INVISIBLE
);
120 bCancel
.setVisibility(View
.GONE
);
121 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
125 protected void setChangePincodeView(boolean state
){
128 bCancel
.setVisibility(View
.VISIBLE
);
129 bCancel
.setOnClickListener(new OnClickListener() {
131 public void onClick(View v
) {
133 SharedPreferences
.Editor appPrefsE
= PreferenceManager
134 .getDefaultSharedPreferences(getApplicationContext()).edit();
136 SharedPreferences appPrefs
= PreferenceManager
137 .getDefaultSharedPreferences(getApplicationContext());
139 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
140 appPrefsE
.putBoolean("set_pincode",!state
);
155 protected void setTextListeners(){
157 /*------------------------------------------------
159 -------------------------------------------------*/
161 mText1
.addTextChangedListener(new TextWatcher() {
164 public void onTextChanged(CharSequence s
, int start
, int before
,
166 // TODO Auto-generated method stub
167 if (s
.length() > 0) {
168 if (!confirmingPinCode
){
169 tempText
[0] = mText1
.getText().toString();
172 mText2
.requestFocus();
177 public void beforeTextChanged(CharSequence s
, int start
, int count
,
179 // TODO Auto-generated method stub
184 public void afterTextChanged(Editable s
) {
185 // TODO Auto-generated method stub
192 /*------------------------------------------------
194 -------------------------------------------------*/
195 mText2
.addTextChangedListener(new TextWatcher() {
198 public void onTextChanged(CharSequence s
, int start
, int before
,
200 // TODO Auto-generated method stub
201 if (s
.length() > 0) {
202 if (!confirmingPinCode
){
203 tempText
[1] = mText2
.getText().toString();
205 mText3
.requestFocus();
210 public void beforeTextChanged(CharSequence s
, int start
, int count
,
212 // TODO Auto-generated method stub
217 public void afterTextChanged(Editable s
) {
218 // TODO Auto-generated method stub
223 mText2
.setOnKeyListener(new OnKeyListener() {
226 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
227 // TODO Auto-generated method stub
229 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
232 mText1
.requestFocus();
244 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
247 public void onFocusChange(View v
, boolean hasFocus
) {
248 // TODO Auto-generated method stub
249 if (mText1
.getText().toString().equals("")){
250 mText1
.requestFocus();
257 /*------------------------------------------------
259 -------------------------------------------------*/
260 mText3
.addTextChangedListener(new TextWatcher() {
263 public void onTextChanged(CharSequence s
, int start
, int before
,
265 // TODO Auto-generated method stub
266 if (s
.length() > 0) {
267 if (!confirmingPinCode
){
268 tempText
[2] = mText3
.getText().toString();
270 mText4
.requestFocus();
275 public void beforeTextChanged(CharSequence s
, int start
, int count
,
277 // TODO Auto-generated method stub
282 public void afterTextChanged(Editable s
) {
283 // TODO Auto-generated method stub
288 mText3
.setOnKeyListener(new OnKeyListener() {
291 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
292 // TODO Auto-generated method stub
294 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
295 mText2
.requestFocus();
308 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
311 public void onFocusChange(View v
, boolean hasFocus
) {
312 // TODO Auto-generated method stub
313 if (mText1
.getText().toString().equals("")){
314 mText1
.requestFocus();
315 }else if (mText2
.getText().toString().equals("")){
316 mText2
.requestFocus();
322 /*------------------------------------------------
324 -------------------------------------------------*/
325 mText4
.addTextChangedListener(new TextWatcher() {
328 public void onTextChanged(CharSequence s
, int start
, int before
,
331 if (s
.length() > 0) {
333 if (!confirmingPinCode
){
334 tempText
[3] = mText4
.getText().toString();
336 mText1
.requestFocus();
338 if (!pinCodeChecked
){
339 pinCodeChecked
= checkPincode();
342 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
344 } else if (pinCodeChecked
){
346 Intent intent
= getIntent();
347 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
349 if (newState
.equals("false")){
350 SharedPreferences
.Editor appPrefs
= PreferenceManager
351 .getDefaultSharedPreferences(getApplicationContext()).edit();
352 appPrefs
.putBoolean("set_pincode",false
);
358 if (!confirmingPinCode
&& !newPasswordEntered
){
359 pinCodeChangeRequest();
360 } else if (newPasswordEntered
&& !confirmingPinCode
){
361 mPinHdr
.setText(R
.string
.pincode_confirm_your_pincode
);
362 confirmingPinCode
= true
;
376 public void beforeTextChanged(CharSequence s
, int start
, int count
,
378 // TODO Auto-generated method stub
383 public void afterTextChanged(Editable s
) {
384 // TODO Auto-generated method stub
391 mText4
.setOnKeyListener(new OnKeyListener() {
394 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
395 // TODO Auto-generated method stub
397 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
398 mText3
.requestFocus();
410 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
413 public void onFocusChange(View v
, boolean hasFocus
) {
414 // TODO Auto-generated method stub
415 if (mText1
.getText().toString().equals("")){
416 mText1
.requestFocus();
417 }else if (mText2
.getText().toString().equals("")){
418 mText2
.requestFocus();
419 }else if (mText3
.getText().toString().equals("")){
420 mText3
.requestFocus();
428 } // end setTextListener
431 protected void pinCodeChangeRequest(){
434 mPinHdr
.setText(R
.string
.pincode_confirm_your_pincode
);
435 confirmingPinCode
=true
;
440 protected boolean checkPincode(){
443 SharedPreferences appPrefs
= PreferenceManager
444 .getDefaultSharedPreferences(getApplicationContext());
446 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
447 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
448 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
449 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
451 if ( tempText
[0].equals(pText1
) &&
452 tempText
[1].equals(pText2
) &&
453 tempText
[2].equals(pText3
) &&
454 tempText
[3].equals(pText4
) ) {
460 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
461 aDialog
.setTitle("ERROR");
462 aDialog
.setMessage("Wrong PIN");
463 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
466 public void onClick(DialogInterface dialog
, int which
) {
467 // TODO Auto-generated method stub("");
474 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
475 newPasswordEntered
= true
;
476 confirmingPinCode
= false
;
484 protected void confirmPincode(){
486 confirmingPinCode
= false
;
488 String rText1
= mText1
.getText().toString();
489 String rText2
= mText2
.getText().toString();
490 String rText3
= mText3
.getText().toString();
491 String rText4
= mText4
.getText().toString();
493 if ( tempText
[0].equals(rText1
) &&
494 tempText
[1].equals(rText2
) &&
495 tempText
[2].equals(rText3
) &&
496 tempText
[3].equals(rText4
) ) {
498 savePincodeAndExit();
502 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
503 aDialog
.setTitle("ERROR");
504 aDialog
.setMessage("PIN Code Mismatch");
505 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
508 public void onClick(DialogInterface dialog
, int which
) {
509 // TODO Auto-generated method stub("");
515 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
522 protected void pinCodeEnd(boolean state
){
523 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
526 aDialog
.setTitle("SAVE & EXIT");
527 aDialog
.setMessage("PIN Code Activated");
529 aDialog
.setTitle("SAVE & EXIT");
530 aDialog
.setMessage("PIN Code Removed");
533 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
536 public void onClick(DialogInterface dialog
, int which
) {
537 // TODO Auto-generated method stub("");
546 protected void savePincodeAndExit(){
547 SharedPreferences
.Editor appPrefs
= PreferenceManager
548 .getDefaultSharedPreferences(getApplicationContext()).edit();
550 appPrefs
.putString("PrefPinCode1", tempText
[0]);
551 appPrefs
.putString("PrefPinCode2",tempText
[1]);
552 appPrefs
.putString("PrefPinCode3", tempText
[2]);
553 appPrefs
.putString("PrefPinCode4", tempText
[3]);
554 appPrefs
.putBoolean("set_pincode",true
);
564 protected void clearBoxes(){
570 mText1
.requestFocus();
575 public boolean onKeyDown(int keyCode
, KeyEvent event
){
576 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
583 return super.onKeyDown(keyCode
, event
);