2 * ownCloud Android client application
4 * @author Bartek Przybylski
6 * @author David A. Velasco
7 * Copyright (C) 2011 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.ui
.activity
;
25 import java
.util
.Arrays
;
27 import android
.content
.SharedPreferences
;
28 import android
.os
.Bundle
;
29 import android
.preference
.PreferenceManager
;
30 import android
.support
.v7
.app
.ActionBar
;
31 import android
.support
.v7
.app
.ActionBarActivity
;
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
.widget
.Button
;
38 import android
.widget
.EditText
;
39 import android
.widget
.TextView
;
40 import android
.widget
.Toast
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
44 import com
.owncloud
.android
.utils
.DisplayUtils
;
46 public class PassCodeActivity
extends ActionBarActivity
{
49 private static final String TAG
= PassCodeActivity
.class.getSimpleName();
51 public final static String ACTION_ENABLE
= PassCodeActivity
.class.getCanonicalName() +
53 public final static String ACTION_DISABLE
= PassCodeActivity
.class.getCanonicalName() +
55 public final static String ACTION_REQUEST
= PassCodeActivity
.class.getCanonicalName() +
58 private Button mBCancel
;
59 private TextView mPassCodeHdr
;
60 private TextView mPassCodeHdrExplanation
;
61 private EditText
[] mPassCodeEditTexts
= new EditText
[4];
63 private String
[] mPassCodeDigits
= {"","","",""};
64 private static String KEY_PASSCODE_DIGITS
= "PASSCODE_DIGITS";
65 private boolean mConfirmingPassCode
= false
;
66 private static String KEY_CONFIRMING_PASSCODE
= "CONFIRMING_PASSCODE";
68 private boolean mBChange
= true
; // to control that only one blocks jump
72 * Initializes the activity.
74 * An intent with a valid ACTION is expected; if none is found, an
75 * {@link IllegalArgumentException} will be thrown.
77 * @param savedInstanceState Previously saved state - irrelevant in this case
79 protected void onCreate(Bundle savedInstanceState
) {
80 super.onCreate(savedInstanceState
);
81 setContentView(R
.layout
.passcodelock
);
83 mBCancel
= (Button
) findViewById(R
.id
.cancel
);
84 mPassCodeHdr
= (TextView
) findViewById(R
.id
.header
);
85 mPassCodeHdrExplanation
= (TextView
) findViewById(R
.id
.explanation
);
86 mPassCodeEditTexts
[0] = (EditText
) findViewById(R
.id
.txt0
);
87 mPassCodeEditTexts
[0].requestFocus();
88 getWindow().setSoftInputMode(
89 android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
90 mPassCodeEditTexts
[1] = (EditText
) findViewById(R
.id
.txt1
);
91 mPassCodeEditTexts
[2] = (EditText
) findViewById(R
.id
.txt2
);
92 mPassCodeEditTexts
[3] = (EditText
) findViewById(R
.id
.txt3
);
94 if (ACTION_REQUEST
.equals(getIntent().getAction())) {
95 /// this is a pass code request; the user has to input the right value
96 mPassCodeHdr
.setText(R
.string
.pass_code_enter_pass_code
);
97 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
98 setCancelButtonEnabled(false
); // no option to cancel
100 } else if (ACTION_ENABLE
.equals(getIntent().getAction())) {
101 if (savedInstanceState
!= null
) {
102 mConfirmingPassCode
= savedInstanceState
.getBoolean(PassCodeActivity
.KEY_CONFIRMING_PASSCODE
);
103 mPassCodeDigits
= savedInstanceState
.getStringArray(PassCodeActivity
.KEY_PASSCODE_DIGITS
);
104 if(mConfirmingPassCode
){
105 //the app was in the passcodeconfirmation
106 requestPassCodeConfirmation();
109 if(savedInstanceState
== null
|| (savedInstanceState
!= null
&& !mConfirmingPassCode
)){
110 /// pass code preference has just been activated in Preferences;
111 // will receive and confirm pass code value
112 mPassCodeHdr
.setText(R
.string
.pass_code_configure_your_pass_code
);
113 //mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);
114 // TODO choose a header, check iOS
115 mPassCodeHdrExplanation
.setVisibility(View
.VISIBLE
);
116 setCancelButtonEnabled(true
);
119 } else if (ACTION_DISABLE
.equals(getIntent().getAction())) {
120 /// pass code preference has just been disabled in Preferences;
121 // will confirm user knows pass code, then remove it
122 mPassCodeHdr
.setText(R
.string
.pass_code_remove_your_pass_code
);
123 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
124 setCancelButtonEnabled(true
);
127 throw new IllegalArgumentException("A valid ACTION is needed in the Intent passed to "
133 ActionBar actionBar
= getSupportActionBar();
134 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
139 * Enables or disables the cancel button to allow the user interrupt the ACTION
140 * requested to the activity.
142 * @param enabled 'True' makes the cancel button available, 'false' hides it.
144 protected void setCancelButtonEnabled(boolean enabled
){
146 mBCancel
.setVisibility(View
.VISIBLE
);
147 mBCancel
.setOnClickListener(new OnClickListener() {
149 public void onClick(View v
) {
150 revertActionAndExit();
154 mBCancel
.setVisibility(View
.GONE
);
155 mBCancel
.setVisibility(View
.INVISIBLE
);
156 mBCancel
.setOnClickListener(null
);
162 * Binds the appropiate listeners to the input boxes receiving each digit of the pass code.
164 protected void setTextListeners() {
166 /// First input field
167 mPassCodeEditTexts
[0].addTextChangedListener(new PassCodeDigitTextWatcher(0, false
));
170 /*------------------------------------------------
172 -------------------------------------------------*/
173 mPassCodeEditTexts
[1].addTextChangedListener(new PassCodeDigitTextWatcher(1, false
));
175 mPassCodeEditTexts
[1].setOnKeyListener(new View
.OnKeyListener() {
178 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
179 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) { // TODO WIP: event should be
180 // used to control what's exactly happening with DEL, not any custom field...
181 mPassCodeEditTexts
[0].setText("");
182 mPassCodeEditTexts
[0].requestFocus();
183 if (!mConfirmingPassCode
)
184 mPassCodeDigits
[0] = "";
187 } else if (!mBChange
) {
194 mPassCodeEditTexts
[1].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
197 public void onFocusChange(View v
, boolean hasFocus
) {
198 /// TODO WIP: should take advantage of hasFocus to reduce processing
199 if (mPassCodeEditTexts
[0].getText().toString().equals("")) { // TODO WIP validation
200 // could be done in a global way, with a single OnFocusChangeListener for all the
202 mPassCodeEditTexts
[0].requestFocus();
208 /*------------------------------------------------
210 -------------------------------------------------*/
211 mPassCodeEditTexts
[2].addTextChangedListener(new PassCodeDigitTextWatcher(2, false
));
213 mPassCodeEditTexts
[2].setOnKeyListener(new View
.OnKeyListener() {
216 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
217 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
218 mPassCodeEditTexts
[1].requestFocus();
219 if (!mConfirmingPassCode
)
220 mPassCodeDigits
[1] = "";
221 mPassCodeEditTexts
[1].setText("");
224 } else if (!mBChange
) {
232 mPassCodeEditTexts
[2].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
235 public void onFocusChange(View v
, boolean hasFocus
) {
236 if (mPassCodeEditTexts
[0].getText().toString().equals("")) {
237 mPassCodeEditTexts
[0].requestFocus();
238 } else if (mPassCodeEditTexts
[1].getText().toString().equals("")) {
239 mPassCodeEditTexts
[1].requestFocus();
245 /*------------------------------------------------
247 -------------------------------------------------*/
248 mPassCodeEditTexts
[3].addTextChangedListener(new PassCodeDigitTextWatcher(3, true
));
250 mPassCodeEditTexts
[3].setOnKeyListener(new View
.OnKeyListener() {
253 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
254 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
255 mPassCodeEditTexts
[2].requestFocus();
256 if (!mConfirmingPassCode
)
257 mPassCodeDigits
[2] = "";
258 mPassCodeEditTexts
[2].setText("");
261 } else if (!mBChange
) {
268 mPassCodeEditTexts
[3].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
271 public void onFocusChange(View v
, boolean hasFocus
) {
273 if (mPassCodeEditTexts
[0].getText().toString().equals("")) {
274 mPassCodeEditTexts
[0].requestFocus();
275 } else if (mPassCodeEditTexts
[1].getText().toString().equals("")) {
276 mPassCodeEditTexts
[1].requestFocus();
277 } else if (mPassCodeEditTexts
[2].getText().toString().equals("")) {
278 mPassCodeEditTexts
[2].requestFocus();
284 } // end setTextListener
288 * Processes the pass code entered by the user just after the last digit was in.
290 * Takes into account the action requested to the activity, the currently saved pass code and
291 * the previously typed pass code, if any.
293 private void processFullPassCode() {
294 if (ACTION_REQUEST
.equals(getIntent().getAction())) {
295 if (checkPassCode()) {
296 /// pass code accepted in request, user is allowed to access the app
300 showErrorAndRestart(R
.string
.pass_code_wrong
, R
.string
.pass_code_enter_pass_code
,
304 } else if (ACTION_DISABLE
.equals(getIntent().getAction())) {
305 if (checkPassCode()) {
306 /// pass code accepted when disabling, pass code is removed
307 SharedPreferences
.Editor appPrefs
= PreferenceManager
308 .getDefaultSharedPreferences(getApplicationContext()).edit();
309 appPrefs
.putBoolean("set_pincode", false
); // TODO remove; this should be
310 // unnecessary, was done before entering in the activity
313 Toast
.makeText(PassCodeActivity
.this, R
.string
.pass_code_removed
, Toast
.LENGTH_LONG
).show();
317 showErrorAndRestart(R
.string
.pass_code_wrong
, R
.string
.pass_code_enter_pass_code
,
321 } else if (ACTION_ENABLE
.equals(getIntent().getAction())) {
322 /// enabling pass code
323 if (!mConfirmingPassCode
) {
324 requestPassCodeConfirmation();
326 } else if (confirmPassCode()) {
327 /// confirmed: user typed the same pass code twice
328 savePassCodeAndExit();
332 R
.string
.pass_code_mismatch
, R
.string
.pass_code_configure_your_pass_code
, View
.VISIBLE
339 private void showErrorAndRestart(int errorMessage
, int headerMessage
,
340 int explanationVisibility
) {
341 Arrays
.fill(mPassCodeDigits
, null
);
342 CharSequence errorSeq
= getString(errorMessage
);
343 Toast
.makeText(this, errorSeq
, Toast
.LENGTH_LONG
).show();
344 mPassCodeHdr
.setText(headerMessage
); // TODO check if really needed
345 mPassCodeHdrExplanation
.setVisibility(explanationVisibility
); // TODO check if really needed
351 * Ask to the user for retyping the pass code just entered before saving it as the current pass
354 protected void requestPassCodeConfirmation(){
356 mPassCodeHdr
.setText(R
.string
.pass_code_reenter_your_pass_code
);
357 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
358 mConfirmingPassCode
= true
;
362 * Compares pass code entered by the user with the value currently saved in the app.
364 * @return 'True' if entered pass code equals to the saved one.
366 protected boolean checkPassCode(){
367 SharedPreferences appPrefs
= PreferenceManager
368 .getDefaultSharedPreferences(getApplicationContext());
370 String savedPassCodeDigits
[] = new String
[4];
371 savedPassCodeDigits
[0] = appPrefs
.getString("PrefPinCode1", null
);
372 savedPassCodeDigits
[1] = appPrefs
.getString("PrefPinCode2", null
);
373 savedPassCodeDigits
[2] = appPrefs
.getString("PrefPinCode3", null
);
374 savedPassCodeDigits
[3] = appPrefs
.getString("PrefPinCode4", null
);
376 boolean result
= true
;
377 for (int i
= 0; i
< mPassCodeDigits
.length
&& result
; i
++) {
378 result
= result
&& (mPassCodeDigits
[i
] != null
) &&
379 mPassCodeDigits
[i
].equals(savedPassCodeDigits
[i
]);
385 * Compares pass code retyped by the user in the input fields with the value entered just
388 * @return 'True' if retyped pass code equals to the entered before.
390 protected boolean confirmPassCode(){
391 mConfirmingPassCode
= false
;
393 boolean result
= true
;
394 for (int i
= 0; i
< mPassCodeEditTexts
.length
&& result
; i
++) {
396 ((mPassCodeEditTexts
[i
].getText().toString()).equals(mPassCodeDigits
[i
]));
402 * Sets the input fields to empty strings and puts the focus on the first one.
404 protected void clearBoxes(){
405 for (int i
=0; i
< mPassCodeEditTexts
.length
; i
++) {
406 mPassCodeEditTexts
[i
].setText("");
408 mPassCodeEditTexts
[0].requestFocus();
412 * Overrides click on the BACK arrow to correctly cancel ACTION_ENABLE or ACTION_DISABLE, while
413 * preventing than ACTION_REQUEST may be worked around.
415 * @param keyCode Key code of the key that triggered the down event.
416 * @param event Event triggered.
417 * @return 'True' when the key event was processed by this method.
420 public boolean onKeyDown(int keyCode
, KeyEvent event
){
421 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
422 if (ACTION_ENABLE
.equals(getIntent().getAction()) ||
423 ACTION_DISABLE
.equals(getIntent().getAction())) {
424 revertActionAndExit();
428 return super.onKeyDown(keyCode
, event
);
432 * Saves the pass code input by the user as the current pass code.
434 protected void savePassCodeAndExit() {
435 SharedPreferences
.Editor appPrefs
= PreferenceManager
436 .getDefaultSharedPreferences(getApplicationContext()).edit();
438 appPrefs
.putString("PrefPinCode1", mPassCodeDigits
[0]);
439 appPrefs
.putString("PrefPinCode2", mPassCodeDigits
[1]);
440 appPrefs
.putString("PrefPinCode3", mPassCodeDigits
[2]);
441 appPrefs
.putString("PrefPinCode4", mPassCodeDigits
[3]);
442 appPrefs
.putBoolean("set_pincode", true
); /// TODO remove; unnecessary,
443 // Preferences did it before entering here
446 Toast
.makeText(this, R
.string
.pass_code_stored
, Toast
.LENGTH_LONG
).show();
451 * Cancellation of ACTION_ENABLE or ACTION_DISABLE; reverts the enable or disable action done by
452 * {@link Preferences}, then finishes.
454 protected void revertActionAndExit() {
455 SharedPreferences
.Editor appPrefsE
= PreferenceManager
456 .getDefaultSharedPreferences(getApplicationContext()).edit();
458 SharedPreferences appPrefs
= PreferenceManager
459 .getDefaultSharedPreferences(getApplicationContext());
461 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
462 appPrefsE
.putBoolean("set_pincode", !state
);
463 // TODO WIP: this is reverting the value of the preference because it was changed BEFORE
465 // TODO in this activity; was the PreferenceCheckBox in the caller who did it
471 public void onSaveInstanceState(Bundle outState
) {
472 super.onSaveInstanceState(outState
);
473 outState
.putBoolean(PassCodeActivity
.KEY_CONFIRMING_PASSCODE
, mConfirmingPassCode
);
474 outState
.putStringArray(PassCodeActivity
.KEY_PASSCODE_DIGITS
, mPassCodeDigits
);
478 private class PassCodeDigitTextWatcher
implements TextWatcher
{
480 private int mIndex
= -1;
481 private boolean mLastOne
= false
;
486 * @param index Position in the pass code of the input field that will be bound to
488 * @param lastOne 'True' means that watcher corresponds to the last position of the
491 public PassCodeDigitTextWatcher(int index
, boolean lastOne
) {
495 throw new IllegalArgumentException(
496 "Invalid index in " + PassCodeDigitTextWatcher
.class.getSimpleName() +
503 return mLastOne ?
0 : mIndex
+ 1;
507 * Performs several actions when the user types a digit in an input field:
508 * - saves the input digit to the state of the activity; this will allow retyping the
509 * pass code to confirm it.
510 * - moves the focus automatically to the next field
511 * - for the last field, triggers the processing of the full pass code
516 public void afterTextChanged(Editable s
) {
517 if (s
.length() > 0) {
518 if (!mConfirmingPassCode
) {
519 mPassCodeDigits
[mIndex
] = mPassCodeEditTexts
[mIndex
].getText().toString();
521 mPassCodeEditTexts
[next()].requestFocus();
524 processFullPassCode();
528 Log_OC
.d(TAG
, "Text box " + mIndex
+ " was cleaned");
533 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
538 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {