2 * ownCloud Android client application
4 * Copyright (C) 2011 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.activity
;
22 import java
.util
.Arrays
;
24 import android
.content
.SharedPreferences
;
25 import android
.os
.Bundle
;
26 import android
.preference
.PreferenceManager
;
27 import android
.text
.Editable
;
28 import android
.text
.TextWatcher
;
29 import android
.view
.KeyEvent
;
30 import android
.view
.View
;
31 import android
.view
.View
.OnClickListener
;
32 import android
.view
.View
.OnFocusChangeListener
;
33 import android
.view
.View
.OnKeyListener
;
34 import android
.widget
.Button
;
35 import android
.widget
.EditText
;
36 import android
.widget
.TextView
;
37 import android
.widget
.Toast
;
39 import com
.actionbarsherlock
.app
.ActionBar
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.utils
.DisplayUtils
;
44 public class PassCodeActivity
extends SherlockFragmentActivity
{
47 public final static String ACTION_TOGGLE
= PassCodeActivity
.class.getCanonicalName() + ".TOGGLE";
48 public final static String ACTION_REQUEST
= PassCodeActivity
.class.getCanonicalName() + ".REQUEST";
50 public final static String EXTRA_NEW_STATE
= PassCodeActivity
.class.getCanonicalName() + ".NEW_STATE";
52 private Button mBCancel
;
53 private TextView mPassCodeHdr
;
54 private TextView mPassCodeHdrExplanation
;
55 private EditText mText1
;
56 private EditText mText2
;
57 private EditText mText3
;
58 private EditText mText4
;
60 private String
[] mTempText
={"","","",""};
62 private boolean mConfirmingPassCode
= false
;
63 private boolean mPassCodeChecked
= false
;
64 private boolean mNewPasswordEntered
= false
;
65 private boolean mBChange
= true
; // to control that only one blocks jump
66 //private int mTCounter ; // Count the number of attempts an user could introduce the pass code
69 protected void onCreate(Bundle savedInstanceState
) {
70 super.onCreate(savedInstanceState
);
71 setContentView(R
.layout
.passcodelock
);
73 mBCancel
= (Button
) findViewById(R
.id
.cancel
);
74 mPassCodeHdr
= (TextView
) findViewById(R
.id
.header
);
75 mPassCodeHdrExplanation
= (TextView
) findViewById(R
.id
.explanation
);
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
);
83 SharedPreferences appPrefs
= PreferenceManager
84 .getDefaultSharedPreferences(getApplicationContext());
87 // Not Pass Code defined yet.
88 // In a previous version settings is allow from start
89 if ( (appPrefs
.getString("PrefPinCode1", null
) == null
) ){
90 setChangePassCodeView(true
);
91 mPassCodeChecked
= true
;
92 mNewPasswordEntered
= true
;
96 /// TODO rewrite this activity; this logic is too twisted
97 if (appPrefs
.getBoolean("set_pincode", false
)){
98 // pass code activated
99 if (ACTION_TOGGLE
.equals(getIntent().getAction())) {
100 // pass code has been activated yet
101 mPassCodeHdr
.setText(R
.string
.pass_code_configure_your_pass_code
);
102 mPassCodeHdrExplanation
.setVisibility(View
.VISIBLE
);
103 mPassCodeChecked
= true
; // No need to check it
104 setChangePassCodeView(true
);
107 mBCancel
.setVisibility(View
.INVISIBLE
);
108 mBCancel
.setVisibility(View
.GONE
);
109 mPassCodeHdr
.setText(R
.string
.pass_code_enter_pass_code
);
110 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
111 setChangePassCodeView(false
);
116 mPassCodeHdr
.setText(R
.string
.pass_code_remove_your_pass_code
);
117 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
118 mPassCodeChecked
= false
;
119 setChangePassCodeView(true
);
125 ActionBar actionBar
= getSupportActionBar();
126 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
131 protected void setInitVars(){
132 mConfirmingPassCode
= false
;
133 mPassCodeChecked
= false
;
134 mNewPasswordEntered
= false
;
138 protected void setInitView(){
139 mBCancel
.setVisibility(View
.INVISIBLE
);
140 mBCancel
.setVisibility(View
.GONE
);
141 mPassCodeHdr
.setText(R
.string
.pass_code_enter_pass_code
);
142 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
146 protected void setChangePassCodeView(boolean state
){
149 mBCancel
.setVisibility(View
.VISIBLE
);
150 mBCancel
.setOnClickListener(new OnClickListener() {
152 public void onClick(View v
) {
154 SharedPreferences
.Editor appPrefsE
= PreferenceManager
155 .getDefaultSharedPreferences(getApplicationContext()).edit();
157 SharedPreferences appPrefs
= PreferenceManager
158 .getDefaultSharedPreferences(getApplicationContext());
160 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
161 appPrefsE
.putBoolean("set_pincode",!state
);
176 protected void setTextListeners(){
178 /*------------------------------------------------
180 -------------------------------------------------*/
182 mText1
.addTextChangedListener(new TextWatcher() {
185 public void onTextChanged(CharSequence s
, int start
, int before
,
190 public void beforeTextChanged(CharSequence s
, int start
, int count
,
195 public void afterTextChanged(Editable s
) {
196 if (s
.length() > 0) {
197 if (!mConfirmingPassCode
){
198 mTempText
[0] = mText1
.getText().toString();
201 mText2
.requestFocus();
208 /*------------------------------------------------
210 -------------------------------------------------*/
211 mText2
.addTextChangedListener(new TextWatcher() {
214 public void onTextChanged(CharSequence s
, int start
, int before
,
219 public void beforeTextChanged(CharSequence s
, int start
, int count
,
224 public void afterTextChanged(Editable s
) {
225 if (s
.length() > 0) {
226 if (!mConfirmingPassCode
){
227 mTempText
[1] = mText2
.getText().toString();
230 mText3
.requestFocus();
235 mText2
.setOnKeyListener(new OnKeyListener() {
238 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
239 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
242 mText1
.requestFocus();
243 if (!mConfirmingPassCode
)
255 mText2
.setOnFocusChangeListener(new OnFocusChangeListener() {
258 public void onFocusChange(View v
, boolean hasFocus
) {
259 mText2
.setCursorVisible(true
);
260 if (mText1
.getText().toString().equals("")){
261 mText2
.setSelected(false
);
262 mText2
.setCursorVisible(false
);
263 mText1
.requestFocus();
264 mText1
.setSelected(true
);
265 mText1
.setSelection(0);
272 /*------------------------------------------------
274 -------------------------------------------------*/
275 mText3
.addTextChangedListener(new TextWatcher() {
278 public void onTextChanged(CharSequence s
, int start
, int before
,
283 public void beforeTextChanged(CharSequence s
, int start
, int count
,
288 public void afterTextChanged(Editable s
) {
289 if (s
.length() > 0) {
290 if (!mConfirmingPassCode
){
291 mTempText
[2] = mText3
.getText().toString();
293 mText4
.requestFocus();
298 mText3
.setOnKeyListener(new OnKeyListener() {
301 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
302 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
303 mText2
.requestFocus();
304 if (!mConfirmingPassCode
)
317 mText3
.setOnFocusChangeListener(new OnFocusChangeListener() {
320 public void onFocusChange(View v
, boolean hasFocus
) {
321 mText3
.setCursorVisible(true
);
322 if (mText1
.getText().toString().equals("")){
323 mText3
.setSelected(false
);
324 mText3
.setCursorVisible(false
);
325 mText1
.requestFocus();
326 mText1
.setSelected(true
);
327 mText1
.setSelection(0);
328 }else if (mText2
.getText().toString().equals("")){
329 mText3
.setSelected(false
);
330 mText3
.setCursorVisible(false
);
331 mText2
.requestFocus();
332 mText2
.setSelected(true
);
333 mText2
.setSelection(0);
339 /*------------------------------------------------
341 -------------------------------------------------*/
342 mText4
.addTextChangedListener(new TextWatcher() {
345 public void onTextChanged(CharSequence s
, int start
, int before
,
350 public void beforeTextChanged(CharSequence s
, int start
, int count
,
355 public void afterTextChanged(Editable s
) {
356 if (s
.length() > 0) {
358 if (!mConfirmingPassCode
){
359 mTempText
[3] = mText4
.getText().toString();
361 mText1
.requestFocus();
363 if (!mPassCodeChecked
){
364 mPassCodeChecked
= checkPassCode();
367 if (mPassCodeChecked
) {
368 if (ACTION_REQUEST
.equals(getIntent().getAction())) {
371 } else if (mPassCodeChecked
) {
372 String newState
= getIntent().getStringExtra(EXTRA_NEW_STATE
);
374 // TODO - next decision should done according to the current state of PIN in prefs (enable or not), not whatever says de client
375 if (newState
.equals("false")) {
376 SharedPreferences
.Editor appPrefs
= PreferenceManager
377 .getDefaultSharedPreferences(getApplicationContext()).edit();
378 appPrefs
.putBoolean("set_pincode", false
);
386 if (!mConfirmingPassCode
) {
387 passCodeChangeRequest();
402 mText4
.setOnKeyListener(new OnKeyListener() {
405 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
406 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
407 mText3
.requestFocus();
408 if (!mConfirmingPassCode
)
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 passCodeChangeRequest(){
457 mPassCodeHdr
.setText(R
.string
.pass_code_reenter_your_pass_code
);
458 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
459 mConfirmingPassCode
=true
;
464 protected boolean checkPassCode(){
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 ( mTempText
[0].equals(pText1
) &&
476 mTempText
[1].equals(pText2
) &&
477 mTempText
[2].equals(pText3
) &&
478 mTempText
[3].equals(pText4
) ) {
484 Arrays
.fill(mTempText
, null
);
485 CharSequence errorSeq
= getString(R
.string
.common_error
);
486 Toast
.makeText(this, errorSeq
, Toast
.LENGTH_LONG
).show();
489 mPassCodeHdr
.setText(R
.string
.pass_code_enter_pass_code
);
490 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
491 mNewPasswordEntered
= true
;
492 mConfirmingPassCode
= false
;
500 protected void confirmPassCode(){
502 mConfirmingPassCode
= 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 ( mTempText
[0].equals(rText1
) &&
510 mTempText
[1].equals(rText2
) &&
511 mTempText
[2].equals(rText3
) &&
512 mTempText
[3].equals(rText4
) ) {
514 savePassCodeAndExit();
517 Arrays
.fill(mTempText
, null
);
518 CharSequence cseq
= getString(R
.string
.pass_code_mismatch
);
519 Toast
.makeText(this, cseq
, Toast
.LENGTH_LONG
).show();
521 mPassCodeHdr
.setText(R
.string
.pass_code_configure_your_pass_code
);
522 mPassCodeHdrExplanation
.setVisibility(View
.VISIBLE
);
529 protected void passCodeEnd(boolean state
){
532 cseq
= getString(R
.string
.pass_code_stored
);
534 cseq
= getString(R
.string
.pass_code_removed
);
537 Toast
.makeText(this, cseq
, Toast
.LENGTH_LONG
).show();
541 protected void savePassCodeAndExit(){
542 SharedPreferences
.Editor appPrefs
= PreferenceManager
543 .getDefaultSharedPreferences(getApplicationContext()).edit();
545 appPrefs
.putString("PrefPinCode1", mTempText
[0]);
546 appPrefs
.putString("PrefPinCode2",mTempText
[1]);
547 appPrefs
.putString("PrefPinCode3", mTempText
[2]);
548 appPrefs
.putString("PrefPinCode4", mTempText
[3]);
549 appPrefs
.putBoolean("set_pincode",true
);
556 protected void clearBoxes(){
561 mText1
.requestFocus();
566 public boolean onKeyDown(int keyCode
, KeyEvent event
){
567 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
568 if (ACTION_TOGGLE
.equals(getIntent().getAction())){
569 SharedPreferences
.Editor appPrefsE
= PreferenceManager
570 .getDefaultSharedPreferences(getApplicationContext()).edit();
572 SharedPreferences appPrefs
= PreferenceManager
573 .getDefaultSharedPreferences(getApplicationContext());
575 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
576 appPrefsE
.putBoolean("set_pincode",!state
);
583 return super.onKeyDown(keyCode
, event
);