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
.AppCompatActivity
;
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 AppCompatActivity
{
49 private static final String TAG
= PassCodeActivity
.class.getSimpleName();
51 public final static String ACTION_ENABLE
= PassCodeActivity
.class.getCanonicalName() + ".ENABLE";
52 public final static String ACTION_DISABLE
= PassCodeActivity
.class.getCanonicalName() + ".DISABLE";
53 public final static String ACTION_REQUEST
= PassCodeActivity
.class.getCanonicalName() + ".REQUEST";
55 private Button mBCancel
;
56 private TextView mPassCodeHdr
;
57 private TextView mPassCodeHdrExplanation
;
58 private EditText
[] mPassCodeEditTexts
= new EditText
[4];
60 private String
[] mPassCodeDigits
= {"","","",""};
61 private boolean mConfirmingPassCode
= false
;
63 private boolean mBChange
= true
; // to control that only one blocks jump
67 * Initializes the activity.
69 * An intent with a valid ACTION is expected; if none is found, an {@link IllegalArgumentException} will be thrown.
71 * @param savedInstanceState Previously saved state - irrelevant in this case
73 protected void onCreate(Bundle savedInstanceState
) {
74 super.onCreate(savedInstanceState
);
75 setContentView(R
.layout
.passcodelock
);
77 mBCancel
= (Button
) findViewById(R
.id
.cancel
);
78 mPassCodeHdr
= (TextView
) findViewById(R
.id
.header
);
79 mPassCodeHdrExplanation
= (TextView
) findViewById(R
.id
.explanation
);
80 mPassCodeEditTexts
[0] = (EditText
) findViewById(R
.id
.txt0
);
81 mPassCodeEditTexts
[0].requestFocus();
82 getWindow().setSoftInputMode(android
.view
.WindowManager
.LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
83 mPassCodeEditTexts
[1] = (EditText
) findViewById(R
.id
.txt1
);
84 mPassCodeEditTexts
[2] = (EditText
) findViewById(R
.id
.txt2
);
85 mPassCodeEditTexts
[3] = (EditText
) findViewById(R
.id
.txt3
);
87 if (ACTION_REQUEST
.equals(getIntent().getAction())) {
88 /// this is a pass code request; the user has to input the right value
89 mPassCodeHdr
.setText(R
.string
.pass_code_enter_pass_code
);
90 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
91 setCancelButtonEnabled(false
); // no option to cancel
93 } else if (ACTION_ENABLE
.equals(getIntent().getAction())) {
94 /// pass code preference has just been activated in Preferences; will receive and confirm pass code value
95 mPassCodeHdr
.setText(R
.string
.pass_code_configure_your_pass_code
);
96 //mPassCodeHdr.setText(R.string.pass_code_enter_pass_code); // TODO choose a header, check iOS
97 mPassCodeHdrExplanation
.setVisibility(View
.VISIBLE
);
98 setCancelButtonEnabled(true
);
100 } else if (ACTION_DISABLE
.equals(getIntent().getAction())) {
101 /// pass code preference has just been disabled in Preferences;
102 // will confirm user knows pass code, then remove it
103 mPassCodeHdr
.setText(R
.string
.pass_code_remove_your_pass_code
);
104 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
105 setCancelButtonEnabled(true
);
108 throw new IllegalArgumentException("A valid ACTION is needed in the Intent passed to " + TAG
);
113 ActionBar actionBar
= getSupportActionBar();
114 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
119 * Enables or disables the cancel button to allow the user interrupt the ACTION requested to the activity.
121 * @param enabled 'True' makes the cancel button available, 'false' hides it.
123 protected void setCancelButtonEnabled(boolean enabled
){
125 mBCancel
.setVisibility(View
.VISIBLE
);
126 mBCancel
.setOnClickListener(new OnClickListener() {
128 public void onClick(View v
) {
129 revertActionAndExit();
133 mBCancel
.setVisibility(View
.GONE
);
134 mBCancel
.setVisibility(View
.INVISIBLE
);
135 mBCancel
.setOnClickListener(null
);
141 * Binds the appropiate listeners to the input boxes receiving each digit of the pass code.
143 protected void setTextListeners() {
145 /// First input field
146 mPassCodeEditTexts
[0].addTextChangedListener(new PassCodeDigitTextWatcher(0, false
));
149 /*------------------------------------------------
151 -------------------------------------------------*/
152 mPassCodeEditTexts
[1].addTextChangedListener(new PassCodeDigitTextWatcher(1, false
));
154 mPassCodeEditTexts
[1].setOnKeyListener(new View
.OnKeyListener() {
157 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
158 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) { // TODO WIP: event should be used to control what's exactly happening with DEL, not any custom field...
159 mPassCodeEditTexts
[0].setText("");
160 mPassCodeEditTexts
[0].requestFocus();
161 if (!mConfirmingPassCode
)
162 mPassCodeDigits
[0] = "";
165 } else if (!mBChange
) {
172 mPassCodeEditTexts
[1].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
175 public void onFocusChange(View v
, boolean hasFocus
) {
176 /// TODO WIP: should take advantage of hasFocus to reduce processing
177 if (mPassCodeEditTexts
[0].getText().toString().equals("")) { // TODO WIP validation could be done in a global way, with a single OnFocusChangeListener for all the input fields
178 mPassCodeEditTexts
[0].requestFocus();
184 /*------------------------------------------------
186 -------------------------------------------------*/
187 mPassCodeEditTexts
[2].addTextChangedListener(new PassCodeDigitTextWatcher(2, false
));
189 mPassCodeEditTexts
[2].setOnKeyListener(new View
.OnKeyListener() {
192 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
193 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
194 mPassCodeEditTexts
[1].requestFocus();
195 if (!mConfirmingPassCode
)
196 mPassCodeDigits
[1] = "";
197 mPassCodeEditTexts
[1].setText("");
200 } else if (!mBChange
) {
208 mPassCodeEditTexts
[2].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
211 public void onFocusChange(View v
, boolean hasFocus
) {
212 if (mPassCodeEditTexts
[0].getText().toString().equals("")) {
213 mPassCodeEditTexts
[0].requestFocus();
214 } else if (mPassCodeEditTexts
[1].getText().toString().equals("")) {
215 mPassCodeEditTexts
[1].requestFocus();
221 /*------------------------------------------------
223 -------------------------------------------------*/
224 mPassCodeEditTexts
[3].addTextChangedListener(new PassCodeDigitTextWatcher(3, true
));
226 mPassCodeEditTexts
[3].setOnKeyListener(new View
.OnKeyListener() {
229 public boolean onKey(View v
, int keyCode
, KeyEvent event
) {
230 if (keyCode
== KeyEvent
.KEYCODE_DEL
&& mBChange
) {
231 mPassCodeEditTexts
[2].requestFocus();
232 if (!mConfirmingPassCode
)
233 mPassCodeDigits
[2] = "";
234 mPassCodeEditTexts
[2].setText("");
237 } else if (!mBChange
) {
244 mPassCodeEditTexts
[3].setOnFocusChangeListener(new View
.OnFocusChangeListener() {
247 public void onFocusChange(View v
, boolean hasFocus
) {
249 if (mPassCodeEditTexts
[0].getText().toString().equals("")) {
250 mPassCodeEditTexts
[0].requestFocus();
251 } else if (mPassCodeEditTexts
[1].getText().toString().equals("")) {
252 mPassCodeEditTexts
[1].requestFocus();
253 } else if (mPassCodeEditTexts
[2].getText().toString().equals("")) {
254 mPassCodeEditTexts
[2].requestFocus();
260 } // end setTextListener
264 * Processes the pass code entered by the user just after the last digit was in.
266 * Takes into account the action requested to the activity, the currently saved pass code and the previously
267 * typed pass code, if any.
269 private void processFullPassCode() {
270 if (ACTION_REQUEST
.equals(getIntent().getAction())) {
271 if (checkPassCode()) {
272 /// pass code accepted in request, user is allowed to access the app
276 showErrorAndRestart(R
.string
.pass_code_wrong
, R
.string
.pass_code_enter_pass_code
, View
.INVISIBLE
);
279 } else if (ACTION_DISABLE
.equals(getIntent().getAction())) {
280 if (checkPassCode()) {
281 /// pass code accepted when disabling, pass code is removed
282 SharedPreferences
.Editor appPrefs
= PreferenceManager
283 .getDefaultSharedPreferences(getApplicationContext()).edit();
284 appPrefs
.putBoolean("set_pincode", false
); // TODO remove; this should be unnecessary, was done before entering in the activity
287 Toast
.makeText(PassCodeActivity
.this, R
.string
.pass_code_removed
, Toast
.LENGTH_LONG
).show();
291 showErrorAndRestart(R
.string
.pass_code_wrong
, R
.string
.pass_code_enter_pass_code
, View
.INVISIBLE
);
294 } else if (ACTION_ENABLE
.equals(getIntent().getAction())) {
295 /// enabling pass code
296 if (!mConfirmingPassCode
) {
297 requestPassCodeConfirmation();
299 } else if (confirmPassCode()) {
300 /// confirmed: user typed the same pass code twice
301 savePassCodeAndExit();
305 R
.string
.pass_code_mismatch
, R
.string
.pass_code_configure_your_pass_code
, View
.VISIBLE
312 private void showErrorAndRestart(int errorMessage
, int headerMessage
, int explanationVisibility
) {
313 Arrays
.fill(mPassCodeDigits
, null
);
314 CharSequence errorSeq
= getString(errorMessage
);
315 Toast
.makeText(this, errorSeq
, Toast
.LENGTH_LONG
).show();
316 mPassCodeHdr
.setText(headerMessage
); // TODO check if really needed
317 mPassCodeHdrExplanation
.setVisibility(explanationVisibility
); // TODO check if really needed
323 * Ask to the user for retyping the pass code just entered before saving it as the current pass code.
325 protected void requestPassCodeConfirmation(){
327 mPassCodeHdr
.setText(R
.string
.pass_code_reenter_your_pass_code
);
328 mPassCodeHdrExplanation
.setVisibility(View
.INVISIBLE
);
329 mConfirmingPassCode
= true
;
333 * Compares pass code entered by the user with the value currently saved in the app.
335 * @return 'True' if entered pass code equals to the saved one.
337 protected boolean checkPassCode(){
338 SharedPreferences appPrefs
= PreferenceManager
339 .getDefaultSharedPreferences(getApplicationContext());
341 String savedPassCodeDigits
[] = new String
[4];
342 savedPassCodeDigits
[0] = appPrefs
.getString("PrefPinCode1", null
);
343 savedPassCodeDigits
[1] = appPrefs
.getString("PrefPinCode2", null
);
344 savedPassCodeDigits
[2] = appPrefs
.getString("PrefPinCode3", null
);
345 savedPassCodeDigits
[3] = appPrefs
.getString("PrefPinCode4", null
);
347 boolean result
= true
;
348 for (int i
= 0; i
< mPassCodeDigits
.length
&& result
; i
++) {
349 result
= result
&& (mPassCodeDigits
[i
] != null
) && mPassCodeDigits
[i
].equals(savedPassCodeDigits
[i
]);
355 * Compares pass code retyped by the user in the input fields with the value entered just before.
357 * @return 'True' if retyped pass code equals to the entered before.
359 protected boolean confirmPassCode(){
360 mConfirmingPassCode
= false
;
362 boolean result
= true
;
363 for (int i
= 0; i
< mPassCodeEditTexts
.length
&& result
; i
++) {
364 result
= result
&& ((mPassCodeEditTexts
[i
].getText().toString()).equals(mPassCodeDigits
[i
]));
370 * Sets the input fields to empty strings and puts the focus on the first one.
372 protected void clearBoxes(){
373 for (int i
=0; i
< mPassCodeEditTexts
.length
; i
++) {
374 mPassCodeEditTexts
[i
].setText("");
376 mPassCodeEditTexts
[0].requestFocus();
380 * Overrides click on the BACK arrow to correctly cancel ACTION_ENABLE or ACTION_DISABLE, while preventing
381 * than ACTION_REQUEST may be worked around.
383 * @param keyCode Key code of the key that triggered the down event.
384 * @param event Event triggered.
385 * @return 'True' when the key event was processed by this method.
388 public boolean onKeyDown(int keyCode
, KeyEvent event
){
389 if (keyCode
== KeyEvent
.KEYCODE_BACK
&& event
.getRepeatCount()== 0){
390 if (ACTION_ENABLE
.equals(getIntent().getAction()) || ACTION_DISABLE
.equals(getIntent().getAction())) {
391 revertActionAndExit();
395 return super.onKeyDown(keyCode
, event
);
399 * Saves the pass code input by the user as the current pass code.
401 protected void savePassCodeAndExit() {
402 SharedPreferences
.Editor appPrefs
= PreferenceManager
403 .getDefaultSharedPreferences(getApplicationContext()).edit();
405 appPrefs
.putString("PrefPinCode1", mPassCodeDigits
[0]);
406 appPrefs
.putString("PrefPinCode2", mPassCodeDigits
[1]);
407 appPrefs
.putString("PrefPinCode3", mPassCodeDigits
[2]);
408 appPrefs
.putString("PrefPinCode4", mPassCodeDigits
[3]);
409 appPrefs
.putBoolean("set_pincode", true
); /// TODO remove; unnecessary, Preferences did it before entering here
412 Toast
.makeText(this, R
.string
.pass_code_stored
, Toast
.LENGTH_LONG
).show();
417 * Cancellation of ACTION_ENABLE or ACTION_DISABLE; reverts the enable or disable action done by
418 * {@link Preferences}, then finishes.
420 protected void revertActionAndExit() {
421 SharedPreferences
.Editor appPrefsE
= PreferenceManager
422 .getDefaultSharedPreferences(getApplicationContext()).edit();
424 SharedPreferences appPrefs
= PreferenceManager
425 .getDefaultSharedPreferences(getApplicationContext());
427 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
428 appPrefsE
.putBoolean("set_pincode", !state
);
429 // TODO WIP: this is reverting the value of the preference because it was changed BEFORE entering
430 // TODO in this activity; was the PreferenceCheckBox in the caller who did it
436 private class PassCodeDigitTextWatcher
implements TextWatcher
{
438 private int mIndex
= -1;
439 private boolean mLastOne
= false
;
444 * @param index Position in the pass code of the input field that will be bound to this watcher.
445 * @param lastOne 'True' means that watcher corresponds to the last position of the pass code.
447 public PassCodeDigitTextWatcher(int index
, boolean lastOne
) {
451 throw new IllegalArgumentException(
452 "Invalid index in " + PassCodeDigitTextWatcher
.class.getSimpleName() + " constructor"
458 return mLastOne ?
0 : mIndex
+ 1;
462 * Performs several actions when the user types a digit in an input field:
463 * - saves the input digit to the state of the activity; this will allow retyping the pass code to confirm it.
464 * - moves the focus automatically to the next field
465 * - for the last field, triggers the processing of the full pass code
470 public void afterTextChanged(Editable s
) {
471 if (s
.length() > 0) {
472 if (!mConfirmingPassCode
) {
473 mPassCodeDigits
[mIndex
] = mPassCodeEditTexts
[mIndex
].getText().toString();
475 mPassCodeEditTexts
[next()].requestFocus();
478 processFullPassCode();
482 Log_OC
.d(TAG
, "Text box " + mIndex
+ " was cleaned");
487 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
492 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {