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 version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.activity
;
19 import java
.util
.Arrays
;
21 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
23 import com
.owncloud
.android
.R
;
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
;
42 public class PinCodeActivity
extends SherlockFragmentActivity
{
45 public final static String EXTRA_ACTIVITY
= "com.owncloud.android.ui.activity.PinCodeActivity.ACTIVITY";
46 public final static String EXTRA_NEW_STATE
= "com.owncloud.android.ui.activity.PinCodeActivity.NEW_STATE";
50 TextView mPinHdrExplanation
;
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 the 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 mPinHdrExplanation
= (TextView
) findViewById(R
.id
.pinHdrExpl
);
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
);
86 SharedPreferences appPrefs
= PreferenceManager
87 .getDefaultSharedPreferences(getApplicationContext());
90 // Not PIN Code defined yet.
91 // In a previous version settings is allow from start
92 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
93 setChangePincodeView(true
);
94 pinCodeChecked
= true
;
95 newPasswordEntered
= true
;
99 if (appPrefs
.getBoolean("set_pincode", false
)){
101 if (activity
.equals("preferences")){
102 // PIN has been activated yet
103 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
104 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
105 pinCodeChecked
= true
; // No need to check it
106 setChangePincodeView(true
);
109 bCancel
.setVisibility(View
.INVISIBLE
);
110 bCancel
.setVisibility(View
.GONE
);
111 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
112 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
113 setChangePincodeView(false
);
118 mPinHdr
.setText(R
.string
.pincode_remove_your_pincode
);
119 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
120 pinCodeChecked
= false
;
121 setChangePincodeView(true
);
132 protected void setInitVars(){
133 confirmingPinCode
= false
;
134 pinCodeChecked
= false
;
135 newPasswordEntered
= false
;
139 protected void setInitView(){
140 bCancel
.setVisibility(View
.INVISIBLE
);
141 bCancel
.setVisibility(View
.GONE
);
142 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
143 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
147 protected void setChangePincodeView(boolean state
){
150 bCancel
.setVisibility(View
.VISIBLE
);
151 bCancel
.setOnClickListener(new OnClickListener() {
153 public void onClick(View v
) {
155 SharedPreferences
.Editor appPrefsE
= PreferenceManager
156 .getDefaultSharedPreferences(getApplicationContext()).edit();
158 SharedPreferences appPrefs
= PreferenceManager
159 .getDefaultSharedPreferences(getApplicationContext());
161 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
162 appPrefsE
.putBoolean("set_pincode",!state
);
177 protected void setTextListeners(){
179 /*------------------------------------------------
181 -------------------------------------------------*/
183 mText1
.addTextChangedListener(new TextWatcher() {
186 public void onTextChanged(CharSequence s
, int start
, int before
,
191 public void beforeTextChanged(CharSequence s
, int start
, int count
,
196 public void afterTextChanged(Editable s
) {
197 if (s
.length() > 0) {
198 if (!confirmingPinCode
){
199 tempText
[0] = mText1
.getText().toString();
202 mText2
.requestFocus();
209 /*------------------------------------------------
211 -------------------------------------------------*/
212 mText2
.addTextChangedListener(new TextWatcher() {
215 public void onTextChanged(CharSequence s
, int start
, int before
,
220 public void beforeTextChanged(CharSequence s
, int start
, int count
,
225 public void afterTextChanged(Editable s
) {
226 if (s
.length() > 0) {
227 if (!confirmingPinCode
){
228 tempText
[1] = mText2
.getText().toString();
231 mText3
.requestFocus();
236 mText2
.setOnKeyListener(new OnKeyListener() {
239 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
240 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
243 mText1
.requestFocus();
244 if (!confirmingPinCode
)
256 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
259 public void onFocusChange(View v
, boolean hasFocus
) {
260 mText2
.setCursorVisible(true
);
261 if (mText1
.getText().toString().equals("")){
262 mText2
.setSelected(false
);
263 mText2
.setCursorVisible(false
);
264 mText1
.requestFocus();
265 mText1
.setSelected(true
);
266 mText1
.setSelection(0);
273 /*------------------------------------------------
275 -------------------------------------------------*/
276 mText3
.addTextChangedListener(new TextWatcher() {
279 public void onTextChanged(CharSequence s
, int start
, int before
,
284 public void beforeTextChanged(CharSequence s
, int start
, int count
,
289 public void afterTextChanged(Editable s
) {
290 if (s
.length() > 0) {
291 if (!confirmingPinCode
){
292 tempText
[2] = mText3
.getText().toString();
294 mText4
.requestFocus();
299 mText3
.setOnKeyListener(new OnKeyListener() {
302 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
303 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
304 mText2
.requestFocus();
305 if (!confirmingPinCode
)
318 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
321 public void onFocusChange(View v
, boolean hasFocus
) {
322 mText3
.setCursorVisible(true
);
323 if (mText1
.getText().toString().equals("")){
324 mText3
.setSelected(false
);
325 mText3
.setCursorVisible(false
);
326 mText1
.requestFocus();
327 mText1
.setSelected(true
);
328 mText1
.setSelection(0);
329 }else if (mText2
.getText().toString().equals("")){
330 mText3
.setSelected(false
);
331 mText3
.setCursorVisible(false
);
332 mText2
.requestFocus();
333 mText2
.setSelected(true
);
334 mText2
.setSelection(0);
340 /*------------------------------------------------
342 -------------------------------------------------*/
343 mText4
.addTextChangedListener(new TextWatcher() {
346 public void onTextChanged(CharSequence s
, int start
, int before
,
351 public void beforeTextChanged(CharSequence s
, int start
, int count
,
356 public void afterTextChanged(Editable s
) {
357 if (s
.length() > 0) {
359 if (!confirmingPinCode
){
360 tempText
[3] = mText4
.getText().toString();
362 mText1
.requestFocus();
364 if (!pinCodeChecked
){
365 pinCodeChecked
= checkPincode();
368 if (pinCodeChecked
&& activity
.equals("FileDisplayActivity")){
370 } else if (pinCodeChecked
){
372 Intent intent
= getIntent();
373 String newState
= intent
.getStringExtra(EXTRA_NEW_STATE
);
375 if (newState
.equals("false")){
376 SharedPreferences
.Editor appPrefs
= PreferenceManager
377 .getDefaultSharedPreferences(getApplicationContext()).edit();
378 appPrefs
.putBoolean("set_pincode",false
);
386 if (!confirmingPinCode
){
387 pinCodeChangeRequest();
402 mText4
.setOnKeyListener(new OnKeyListener() {
405 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
406 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& bChange
) {
407 mText3
.requestFocus();
408 if (!confirmingPinCode
)
420 mText4
.setOnFocusChangeListener(new OnFocusChangeListener() {
423 public void onFocusChange(View v
, boolean hasFocus
) {
424 mText4
.setCursorVisible(true
);
426 if (mText1
.getText().toString().equals("")){
427 mText4
.setSelected(false
);
428 mText4
.setCursorVisible(false
);
429 mText1
.requestFocus();
430 mText1
.setSelected(true
);
431 mText1
.setSelection(0);
432 }else if (mText2
.getText().toString().equals("")){
433 mText4
.setSelected(false
);
434 mText4
.setCursorVisible(false
);
435 mText2
.requestFocus();
436 mText2
.setSelected(true
);
437 mText2
.setSelection(0);
438 }else if (mText3
.getText().toString().equals("")){
439 mText4
.setSelected(false
);
440 mText4
.setCursorVisible(false
);
441 mText3
.requestFocus();
442 mText3
.setSelected(true
);
443 mText3
.setSelection(0);
451 } // end setTextListener
454 protected void pinCodeChangeRequest(){
457 mPinHdr
.setText(R
.string
.pincode_reenter_your_pincode
);
458 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
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 CharSequence errorSeq
= getString(R
.string
.common_error
);
487 aDialog
.setTitle(errorSeq
);
488 CharSequence cseq
= getString(R
.string
.pincode_wrong
);
489 aDialog
.setMessage(cseq
);
490 CharSequence okSeq
= getString(R
.string
.common_ok
);
491 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
494 public void onClick(DialogInterface dialog
, int which
) {
501 mPinHdr
.setText(R
.string
.pincode_enter_pin_code
);
502 mPinHdrExplanation
.setVisibility(View
.INVISIBLE
);
503 newPasswordEntered
= true
;
504 confirmingPinCode
= false
;
512 protected void confirmPincode(){
514 confirmingPinCode
= false
;
516 String rText1
= mText1
.getText().toString();
517 String rText2
= mText2
.getText().toString();
518 String rText3
= mText3
.getText().toString();
519 String rText4
= mText4
.getText().toString();
521 if ( tempText
[0].equals(rText1
) &&
522 tempText
[1].equals(rText2
) &&
523 tempText
[2].equals(rText3
) &&
524 tempText
[3].equals(rText4
) ) {
526 savePincodeAndExit();
530 Arrays
.fill(tempText
, null
);
531 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
532 CharSequence errorSeq
= getString(R
.string
.common_error
);
533 aDialog
.setTitle(errorSeq
);
534 CharSequence cseq
= getString(R
.string
.pincode_mismatch
);
535 aDialog
.setMessage(cseq
);
536 CharSequence okSeq
= getString(R
.string
.common_ok
);
537 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
540 public void onClick(DialogInterface dialog
, int which
) {
546 mPinHdr
.setText(R
.string
.pincode_configure_your_pin
);
547 mPinHdrExplanation
.setVisibility(View
.VISIBLE
);
554 protected void pinCodeEnd(boolean state
){
555 AlertDialog aDialog
= new AlertDialog
.Builder(this).create();
558 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
559 aDialog
.setTitle(saveSeq
);
560 CharSequence cseq
= getString(R
.string
.pincode_stored
);
561 aDialog
.setMessage(cseq
);
564 CharSequence saveSeq
= getString(R
.string
.common_save_exit
);
565 aDialog
.setTitle(saveSeq
);
566 CharSequence cseq
= getString(R
.string
.pincode_removed
);
567 aDialog
.setMessage(cseq
);
570 CharSequence okSeq
= getString(R
.string
.common_ok
);
571 aDialog
.setButton(okSeq
, new DialogInterface
.OnClickListener(){
574 public void onClick(DialogInterface dialog
, int which
) {
583 protected void savePincodeAndExit(){
584 SharedPreferences
.Editor appPrefs
= PreferenceManager
585 .getDefaultSharedPreferences(getApplicationContext()).edit();
587 appPrefs
.putString("PrefPinCode1", tempText
[0]);
588 appPrefs
.putString("PrefPinCode2",tempText
[1]);
589 appPrefs
.putString("PrefPinCode3", tempText
[2]);
590 appPrefs
.putString("PrefPinCode4", tempText
[3]);
591 appPrefs
.putBoolean("set_pincode",true
);
601 protected void clearBoxes(){
607 mText1
.requestFocus();
612 public boolean onKeyDown(int keyCode
, KeyEvent event
){
613 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
615 if (activity
.equals("preferences")){
616 SharedPreferences
.Editor appPrefsE
= PreferenceManager
618 .getDefaultSharedPreferences(getApplicationContext()).edit();
620 SharedPreferences appPrefs
= PreferenceManager
621 .getDefaultSharedPreferences(getApplicationContext());
623 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
624 appPrefsE
.putBoolean("set_pincode",!state
);
633 return super.onKeyDown(keyCode
, event
);