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
.ActionBar
.LayoutParams
;
25 import android
.app
.AlertDialog
;
26 import android
.content
.DialogInterface
;
27 import android
.content
.Intent
;
28 import android
.content
.SharedPreferences
;
29 import android
.os
.Bundle
;
30 import android
.preference
.PreferenceManager
;
31 import android
.text
.Editable
;
32 import android
.text
.TextWatcher
;
33 import android
.view
.KeyEvent
;
34 import android
.view
.View
;
35 import android
.view
.View
.OnClickListener
;
36 import android
.view
.View
.OnFocusChangeListener
;
37 import android
.view
.View
.OnKeyListener
;
38 import android
.widget
.Button
;
39 import android
.widget
.EditText
;
40 import android
.widget
.TextView
;
43 public class PinCodeActivity
extends SherlockFragmentActivity
{
46 public final static String EXTRA_ACTIVITY
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
47 public final static String EXTRA_NEW_STATE
= "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
56 String
[] tempText
={"","","",""};
60 boolean confirmingPinCode
= false
;
61 boolean pinCodeChecked
= false
;
62 boolean newPasswordEntered
= false
;
63 boolean bChange
= true
; // to control that only one blocks jump
64 int tCounter
; // Count the number of attempts an user could introduce de PIN code
67 protected void onCreate(Bundle savedInstanceState
) {
68 super.onCreate(savedInstanceState
);
69 setContentView(R
.layout
.pincodelock
);
71 Intent intent
= getIntent();
72 activity
= intent
.getStringExtra(EXTRA_ACTIVITY
);
74 bCancel
= (Button
) findViewById(R
.id
.cancel
);
75 mPinHdr
= (TextView
) findViewById(R
.id
.pinHdr
);
76 mText1
= (EditText
) findViewById(R
.id
.txt1
);
77 mText1
.requestFocus();
78 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
79 mText2
= (EditText
) findViewById(R
.id
.txt2
);
80 mText3
= (EditText
) findViewById(R
.id
.txt3
);
81 mText4
= (EditText
) findViewById(R
.id
.txt4
);
84 SharedPreferences appPrefs
= PreferenceManager
85 .getDefaultSharedPreferences(getApplicationContext());
87 // Not PIN Code defined yet
88 if ( appPrefs
.getString("PrefPinCode1", null
) == null
){
89 setChangePincodeView();
90 pinCodeChecked
= true
;
91 newPasswordEntered
= true
;
104 protected void setInitView(){
105 bCancel
.setVisibility(View
.INVISIBLE
);
106 bCancel
.setVisibility(View
.GONE
);
107 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
112 protected void setChangePincodeView(){
113 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
114 bCancel
.setOnClickListener(new OnClickListener() {
117 public void onClick(View v
) {
127 protected void setTextListeners(){
129 /*------------------------------------------------
131 -------------------------------------------------*/
133 mText1
.addTextChangedListener(new TextWatcher() {
136 public void onTextChanged(CharSequence s
, int start
, int before
,
138 // TODO Auto-generated method stub
139 if (s
.length() > 0) {
140 if (!confirmingPinCode
){
141 tempText
[0] = mText1
.getText().toString();
144 mText2
.requestFocus();
149 public void beforeTextChanged(CharSequence s
, int start
, int count
,
151 // TODO Auto-generated method stub
156 public void afterTextChanged(Editable s
) {
157 // TODO Auto-generated method stub
164 /*------------------------------------------------
166 -------------------------------------------------*/
167 mText2
.addTextChangedListener(new TextWatcher() {
170 public void onTextChanged(CharSequence s
, int start
, int before
,
172 // TODO Auto-generated method stub
173 if (s
.length() > 0) {
174 if (!confirmingPinCode
){
175 tempText
[1] = mText2
.getText().toString();
177 mText3
.requestFocus();
182 public void beforeTextChanged(CharSequence s
, int start
, int count
,
184 // TODO Auto-generated method stub
189 public void afterTextChanged(Editable s
) {
190 // TODO Auto-generated method stub
195 mText2
.setOnKeyListener(new OnKeyListener() {
198 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
199 // TODO Auto-generated method stub
201 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
204 mText1
.requestFocus();
216 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
219 public void onFocusChange(View v
, boolean hasFocus
) {
220 // TODO Auto-generated method stub
221 if (mText1
.getText().toString().equals("")){
222 mText1
.requestFocus();
229 /*------------------------------------------------
231 -------------------------------------------------*/
232 mText3
.addTextChangedListener(new TextWatcher() {
235 public void onTextChanged(CharSequence s
, int start
, int before
,
237 // TODO Auto-generated method stub
238 if (s
.length() > 0) {
239 if (!confirmingPinCode
){
240 tempText
[2] = mText3
.getText().toString();
242 mText4
.requestFocus();
247 public void beforeTextChanged(CharSequence s
, int start
, int count
,
249 // TODO Auto-generated method stub
254 public void afterTextChanged(Editable s
) {
255 // TODO Auto-generated method stub
260 mText3
.setOnKeyListener(new OnKeyListener() {
263 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
264 // TODO Auto-generated method stub
266 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
267 mText2
.requestFocus();
280 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
283 public void onFocusChange(View v
, boolean hasFocus
) {
284 // TODO Auto-generated method stub
285 if (mText1
.getText().toString().equals("")){
286 mText1
.requestFocus();
287 }else if (mText2
.getText().toString().equals("")){
288 mText2
.requestFocus();
294 /*------------------------------------------------
296 -------------------------------------------------*/
297 mText4
.addTextChangedListener(new TextWatcher() {
300 public void onTextChanged(CharSequence s
, int start
, int before
,
303 if (s
.length() > 0) {
305 if (!confirmingPinCode
){
306 tempText
[3] = mText4
.getText().toString();
308 mText1
.requestFocus();
310 if (!pinCodeChecked
){
311 pinCodeChecked
= checkPincode();
314 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
316 } else if (pinCodeChecked
){
318 Intent intent
= getIntent();
319 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
321 if (newState
.equals("false")){
322 SharedPreferences
.Editor appPrefs
= PreferenceManager
323 .getDefaultSharedPreferences(getApplicationContext()).edit();
324 appPrefs
.putBoolean("set_pincode",false
);
326 // TODO Alert Message que salte y vuelva a la pantalla anterior
332 if (!confirmingPinCode
&& !newPasswordEntered
){
333 pinCodeChangeRequest();
334 } else if (newPasswordEntered
&& !confirmingPinCode
){
335 mPinHdr
.setText(R
.string
.pincode_confirm_your_pincode
);
336 confirmingPinCode
= true
;
350 public void beforeTextChanged(CharSequence s
, int start
, int count
,
352 // TODO Auto-generated method stub
357 public void afterTextChanged(Editable s
) {
358 // TODO Auto-generated method stub
365 mText4
.setOnKeyListener(new OnKeyListener() {
368 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
369 // TODO Auto-generated method stub
371 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
372 mText3
.requestFocus();
384 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
387 public void onFocusChange(View v
, boolean hasFocus
) {
388 // TODO Auto-generated method stub
389 if (mText1
.getText().toString().equals("")){
390 mText1
.requestFocus();
391 }else if (mText2
.getText().toString().equals("")){
392 mText2
.requestFocus();
393 }else if (mText3
.getText().toString().equals("")){
394 mText3
.requestFocus();
402 } // end setTextListener
405 protected void pinCodeChangeRequest(){
407 AlertDialog
.Builder aBuilder
= new AlertDialog
.Builder(this);
408 aBuilder
.setMessage("Do yo want to set a new PIN Code")
409 .setCancelable(false
)
410 .setPositiveButton("Yes", new DialogInterface
.OnClickListener() {
413 public void onClick(DialogInterface dialog
, int which
) {
414 // TODO Auto-generated method stub
415 setChangePincodeView();
416 mPinHdr
.setText(R
.string
.pincode_enter_new_pin_code
);
418 newPasswordEntered
= true
;
421 .setNegativeButton("No", new DialogInterface
.OnClickListener() {
424 public void onClick(DialogInterface dialog
, int which
) {
425 // TODO Auto-generated method stub
426 SharedPreferences
.Editor appPrefs
= PreferenceManager
427 .getDefaultSharedPreferences(getApplicationContext()).edit();
428 appPrefs
.putBoolean("set_pincode",false
);
434 AlertDialog alert
=aBuilder
.create();
442 protected boolean checkPincode(){
445 SharedPreferences appPrefs
= PreferenceManager
446 .getDefaultSharedPreferences(getApplicationContext());
448 String pText1
= appPrefs
.getString("PrefPinCode1", null
);
449 String pText2
= appPrefs
.getString("PrefPinCode2", null
);
450 String pText3
= appPrefs
.getString("PrefPinCode3", null
);
451 String pText4
= appPrefs
.getString("PrefPinCode4", null
);
453 if ( tempText
[0].equals(pText1
) &&
454 tempText
[1].equals(pText2
) &&
455 tempText
[2].equals(pText3
) &&
456 tempText
[3].equals(pText4
) ) {
462 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
463 aDialog
.setTitle("ERROR");
464 aDialog
.setMessage("Wrong PIN");
465 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
468 public void onClick(DialogInterface dialog
, int which
) {
469 // TODO Auto-generated method stub("");
476 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
477 newPasswordEntered
= true
;
478 confirmingPinCode
= false
;
486 protected void confirmPincode(){
488 confirmingPinCode
= false
;
490 String rText1
= mText1
.getText().toString();
491 String rText2
= mText2
.getText().toString();
492 String rText3
= mText3
.getText().toString();
493 String rText4
= mText4
.getText().toString();
495 if ( tempText
[0].equals(rText1
) &&
496 tempText
[1].equals(rText2
) &&
497 tempText
[2].equals(rText3
) &&
498 tempText
[3].equals(rText4
) ) {
500 savePincodeAndExit();
504 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
505 aDialog
.setTitle("ERROR");
506 aDialog
.setMessage("PIN Code Mismatch");
507 aDialog
.setButton("OK", new DialogInterface
.OnClickListener(){
510 public void onClick(DialogInterface dialog
, int which
) {
511 // TODO Auto-generated method stub("");
517 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
523 protected void savePincodeAndExit(){
524 SharedPreferences
.Editor appPrefs
= PreferenceManager
525 .getDefaultSharedPreferences(getApplicationContext()).edit();
527 appPrefs
.putString("PrefPinCode1", tempText
[0]);
528 appPrefs
.putString("PrefPinCode2",tempText
[1]);
529 appPrefs
.putString("PrefPinCode3", tempText
[2]);
530 appPrefs
.putString("PrefPinCode4", tempText
[3]);
531 appPrefs
.putBoolean("set_pincode",true
);
538 protected void clearBoxes(){
544 mText1
.requestFocus();