Prechádzať zdrojové kódy

Cleaned code in PassCodeActivity

David A. Velasco 10 rokov pred
rodič
commit
510c00f66d

+ 4 - 4
res/layout/passcodelock.xml

@@ -50,22 +50,22 @@
         android:gravity="center_horizontal" >
 
         <EditText
-            android:id="@+id/txt1"
+            android:id="@+id/txt0"
             android:focusable="true"
             style="@style/PassCodeStyle"
             android:cursorVisible="true"
             ><requestFocus/></EditText>
 
         <EditText
-            android:id="@+id/txt2"
+            android:id="@+id/txt1"
             style="@style/PassCodeStyle" />
 
         <EditText
-            android:id="@+id/txt3"
+            android:id="@+id/txt2"
             style="@style/PassCodeStyle" />
 
         <EditText
-            android:id="@+id/txt4"
+            android:id="@+id/txt3"
             style="@style/PassCodeStyle" />
     </LinearLayout>
 

+ 1 - 1
res/xml/preferences.xml

@@ -22,7 +22,7 @@
     </PreferenceCategory>
     
 	<PreferenceCategory android:title="@string/prefs_category_security">
-	    <android.preference.CheckBoxPreference android:title="@string/prefs_pincode" android:key="set_pincode" />
+	    <android.preference.CheckBoxPreference android:title="@string/prefs_passcode" android:key="set_pincode" />
 	</PreferenceCategory>
 
     <PreferenceCategory android:title="@string/prefs_category_instant_uploading" android:key="instant_uploading_category">

+ 330 - 338
src/com/owncloud/android/ui/activity/PassCodeActivity.java

@@ -39,33 +39,39 @@ import android.widget.Toast;
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.owncloud.android.R;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.utils.DisplayUtils;
 
 public class PassCodeActivity extends SherlockFragmentActivity {
 
-  
-    public final static String ACTION_TOGGLE = PassCodeActivity.class.getCanonicalName() + ".TOGGLE";
-    public final static String ACTION_REQUEST= PassCodeActivity.class.getCanonicalName()  + ".REQUEST";
 
-    public final static String EXTRA_NEW_STATE = PassCodeActivity.class.getCanonicalName() + ".NEW_STATE";
-    
+    private static final String TAG = PassCodeActivity.class.getSimpleName();
+
+    public final static String ACTION_ENABLE = PassCodeActivity.class.getCanonicalName() + ".ENABLE";
+    public final static String ACTION_DISABLE = PassCodeActivity.class.getCanonicalName() + ".DISABLE";
+    public final static String ACTION_REQUEST = PassCodeActivity.class.getCanonicalName()  + ".REQUEST";
+
     private Button mBCancel;
     private TextView mPassCodeHdr;
     private TextView mPassCodeHdrExplanation;
+    private EditText mText0;
     private EditText mText1;
     private EditText mText2;
     private EditText mText3;
-    private EditText mText4;
-    
-    private String [] mTempText ={"","","",""};
     
+    private String [] mPassCodeDigits = {"","","",""};
     private boolean mConfirmingPassCode = false;
-    private boolean mPassCodeChecked = false;
-    private boolean mNewPasswordEntered = false;
+
     private boolean mBChange = true; // to control that only one blocks jump
-    //private int mTCounter ; // Count the number of attempts an user could introduce the pass code
 
-    
+
+    /**
+     * Initializes the activity.
+     *
+     * An intent with a valid ACTION is expected; if none is found, an {@link IllegalArgumentException} will be thrown.
+     *
+     * @param savedInstanceState    Previously saved state - irrelevant in this case
+     */
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.passcodelock);
@@ -73,53 +79,37 @@ public class PassCodeActivity extends SherlockFragmentActivity {
         mBCancel = (Button) findViewById(R.id.cancel);
         mPassCodeHdr = (TextView) findViewById(R.id.header);
         mPassCodeHdrExplanation = (TextView) findViewById(R.id.explanation);
-        mText1 = (EditText) findViewById(R.id.txt1);
-        mText1.requestFocus();
+        mText0 = (EditText) findViewById(R.id.txt0);
+        mText0.requestFocus();
         getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        mText1 = (EditText) findViewById(R.id.txt1);
         mText2 = (EditText) findViewById(R.id.txt2);
         mText3 = (EditText) findViewById(R.id.txt3);
-        mText4 = (EditText) findViewById(R.id.txt4);
-        
-        SharedPreferences appPrefs = PreferenceManager
-                .getDefaultSharedPreferences(getApplicationContext());
-        
- 
-        // Not Pass Code defined yet.
-        // In a previous version settings is allow from start
-        if ( (appPrefs.getString("PrefPinCode1", null) == null ) ){
-            setChangePassCodeView(true);
-            mPassCodeChecked = true;
-            mNewPasswordEntered = true;
-            
-        } else {
 
-            /// TODO rewrite this activity; this logic is too twisted
-            if (appPrefs.getBoolean("set_pincode", false)){
-                // pass code activated
-                if (ACTION_TOGGLE.equals(getIntent().getAction())) {
-                    // pass code has been activated yet
-                    mPassCodeHdr.setText(R.string.pass_code_configure_your_pass_code);
-                    mPassCodeHdrExplanation.setVisibility(View.VISIBLE);
-                    mPassCodeChecked = true ; // No need to check it
-                    setChangePassCodeView(true);
-               } else {
-                    // pass code active
-                    mBCancel.setVisibility(View.INVISIBLE);
-                    mBCancel.setVisibility(View.GONE);
-                    mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);
-                    mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
-                    setChangePassCodeView(false);
-              }
-            
-           } else {
-                // pass code removal
-                mPassCodeHdr.setText(R.string.pass_code_remove_your_pass_code);
-                mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
-                mPassCodeChecked = false;
-                setChangePassCodeView(true);
-           }
-           
+        if (ACTION_REQUEST.equals(getIntent().getAction())) {
+            /// this is a pass code request; the user has to input the right value
+            mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);
+            mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
+            setCancelButtonEnabled(false);      // no option to cancel
+
+        } else if (ACTION_ENABLE.equals(getIntent().getAction())) {
+            /// pass code preference has just been activated in Preferences; will receive and confirm pass code value
+            mPassCodeHdr.setText(R.string.pass_code_configure_your_pass_code);
+            //mPassCodeHdr.setText(R.string.pass_code_enter_pass_code); // TODO choose a header, check iOS
+            mPassCodeHdrExplanation.setVisibility(View.VISIBLE);
+            setCancelButtonEnabled(true);
+
+        } else if (ACTION_DISABLE.equals(getIntent().getAction())) {
+            /// pass code preference has just been disabled in Preferences;
+            // will confirm user knows pass code, then remove it
+            mPassCodeHdr.setText(R.string.pass_code_remove_your_pass_code);
+            mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
+            setCancelButtonEnabled(true);
+
+        } else {
+            throw new IllegalArgumentException("A valid ACTION is needed in the Intent passed to " + TAG);
         }
+
         setTextListeners();
         
         ActionBar actionBar = getSupportActionBar();
@@ -127,49 +117,24 @@ public class PassCodeActivity extends SherlockFragmentActivity {
     }
     
 
-     
-    protected void setInitVars(){
-        mConfirmingPassCode = false;
-        mPassCodeChecked = false;
-        mNewPasswordEntered = false;
-
-    }
-    
-    protected void setInitView(){
-        mBCancel.setVisibility(View.INVISIBLE);
-        mBCancel.setVisibility(View.GONE);
-        mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);
-        mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
-    }
-    
-   
-    protected void setChangePassCodeView(boolean state){
-       
-        if(state){
-        mBCancel.setVisibility(View.VISIBLE);
-        mBCancel.setOnClickListener(new OnClickListener() {
-        @Override
-        public void onClick(View v) {
-            
-            SharedPreferences.Editor appPrefsE = PreferenceManager
-                    .getDefaultSharedPreferences(getApplicationContext()).edit();
-            
-            SharedPreferences appPrefs = PreferenceManager
-                    .getDefaultSharedPreferences(getApplicationContext());
-            
-            boolean state = appPrefs.getBoolean("set_pincode", false);
-            appPrefsE.putBoolean("set_pincode",!state); 
-            appPrefsE.commit();
-            setInitVars();
-            finish();
-            }
-        });
-        }  
+    protected void setCancelButtonEnabled(boolean enabled){
+        if(enabled){
+            mBCancel.setVisibility(View.VISIBLE);
+            mBCancel.setOnClickListener(new OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    revertActionAndExit();
+                }
+            });
+        } else {
+            mBCancel.setVisibility(View.GONE);
+            mBCancel.setVisibility(View.INVISIBLE);
+            mBCancel.setOnClickListener(null);
+        }
     
     }
     
     
-    
     /*
      *  
      */
@@ -179,27 +144,28 @@ public class PassCodeActivity extends SherlockFragmentActivity {
          *  FIRST BOX
          -------------------------------------------------*/
         
-        mText1.addTextChangedListener(new TextWatcher() {
+        mText0.addTextChangedListener(new TextWatcher() {
 
             @Override
             public void onTextChanged(CharSequence s, int start, int before,
-                    int count) {
+                                      int count) {
             }
 
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,
-                    int after) {
+                                          int after) {
             }
 
             @Override
             public void afterTextChanged(Editable s) {
                 if (s.length() > 0) {
-                    if (!mConfirmingPassCode){
-                       mTempText[0] = mText1.getText().toString();
-                       
+                    if (!mConfirmingPassCode) {
+                        mPassCodeDigits[0] = mText0.getText().toString();
                     }
-                    mText2.requestFocus();
-                 }
+                    mText1.requestFocus();
+                } else {
+                    Log_OC.w(TAG, "Input in text box 0 resulted in empty string");
+                }
             }
         });
         
@@ -208,63 +174,62 @@ public class PassCodeActivity extends SherlockFragmentActivity {
         /*------------------------------------------------
          *  SECOND BOX 
          -------------------------------------------------*/
-        mText2.addTextChangedListener(new TextWatcher() {
+        mText1.addTextChangedListener(new TextWatcher() {
 
             @Override
             public void onTextChanged(CharSequence s, int start, int before,
-                    int count) {
+                                      int count) {
             }
 
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,
-                    int after) {
+                                          int after) {
             }
 
             @Override
             public void afterTextChanged(Editable s) {
                 if (s.length() > 0) {
-                    if (!mConfirmingPassCode){
-                        mTempText[1] = mText2.getText().toString();
+                    if (!mConfirmingPassCode) {
+                        mPassCodeDigits[1] = mText1.getText().toString();
                     }
-                    
-                    mText3.requestFocus();
+                    mText2.requestFocus();
+                } else {
+                    Log_OC.w(TAG, "Input in text box 1 resulted in empty string");
                 }
             }
         });
  
-        mText2.setOnKeyListener(new OnKeyListener() {
+        mText1.setOnKeyListener(new OnKeyListener() {
 
             @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
-                if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
-
-                    mText1.setText("");
-                    mText1.requestFocus();
+                if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {  // TODO WIP: event should be used to control what's exactly happening with DEL, not any custom field...
+                    mText0.setText("");
+                    mText0.requestFocus();
                     if (!mConfirmingPassCode)
-                       mTempText[0] = "";
-                    mBChange= false;
-                
-                }else if(!mBChange){
-                    mBChange=true;
-                    
+                        mPassCodeDigits[0] = "";    // TODO WIP: what is this for??
+                    mBChange = false;
+
+                } else if (!mBChange) {
+                    mBChange = true;
                 }
                 return false;
             }
         });        
  
-        mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
-               
+        mText1.setOnFocusChangeListener(new OnFocusChangeListener() {
+
             @Override
             public void onFocusChange(View v, boolean hasFocus) {
-                mText2.setCursorVisible(true);
-                if (mText1.getText().toString().equals("")){
-                    mText2.setSelected(false);
-                    mText2.setCursorVisible(false);
-                    mText1.requestFocus(); 
-                    mText1.setSelected(true);
-                    mText1.setSelection(0);
+                mText1.setCursorVisible(true);      // TODO WIP this could be made static, or just nothing, since default is true...
+                if (mText0.getText().toString().equals("")) {    // TODO WIP is this really needed? when?
+                    mText1.setSelected(false);
+                    mText1.setCursorVisible(false); // TODO WIP really this is a problem?
+                    mText0.requestFocus();  // TODO WIP how many focus requests do we need?
+                    mText0.setSelected(true);   // TODO WIP what is this for?
+                    mText0.setSelection(0);     // TODO WIP what is THIS for?
                 }
-                
+
             }
         });
         
@@ -272,314 +237,341 @@ public class PassCodeActivity extends SherlockFragmentActivity {
         /*------------------------------------------------
          *  THIRD BOX
          -------------------------------------------------*/
-        mText3.addTextChangedListener(new TextWatcher() {
+        /// TODO WIP yeah, let's repeat all the code again...
+        mText2.addTextChangedListener(new TextWatcher() {
 
             @Override
             public void onTextChanged(CharSequence s, int start, int before,
-                    int count) {
+                                      int count) {
             }
 
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,
-                    int after) {
+                                          int after) {
             }
 
             @Override
             public void afterTextChanged(Editable s) {
                 if (s.length() > 0) {
-                    if (!mConfirmingPassCode){
-                        mTempText[2] = mText3.getText().toString();
+                    if (!mConfirmingPassCode) {
+                        mPassCodeDigits[2] = mText2.getText().toString();
                     }
-                    mText4.requestFocus();
+                    mText3.requestFocus();
+                } else {
+                    Log_OC.w(TAG, "Input in text box 2 resulted in empty string");
                 }
             }
         });
         
-        mText3.setOnKeyListener(new OnKeyListener() {
+        mText2.setOnKeyListener(new OnKeyListener() {
 
             @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
-                    mText2.requestFocus();
+                    mText1.requestFocus();
                     if (!mConfirmingPassCode)
-                        mTempText[1] = "";
-                    mText2.setText("");
-                    mBChange= false;
-                    
-                }else if(!mBChange){
-                    mBChange=true;                        
-                    
+                        mPassCodeDigits[1] = "";
+                    mText1.setText("");
+                    mBChange = false;
+
+                } else if (!mBChange) {
+                    mBChange = true;
+
                 }
                 return false;
             }
         });
-        
-        mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
-            
+
+        mText2.setOnFocusChangeListener(new OnFocusChangeListener() {
+
             @Override
             public void onFocusChange(View v, boolean hasFocus) {
-                mText3.setCursorVisible(true);
-                if (mText1.getText().toString().equals("")){
-                    mText3.setSelected(false);
-                    mText3.setCursorVisible(false);
+                /// TODO WIP: hasFocus is there for some reason; for instance, doing NOTHING if this is not my business, instead of considering all the possible cases in every edit text
+                mText2.setCursorVisible(true);
+                if (mText0.getText().toString().equals("")) {
+                    mText2.setSelected(false);
+                    mText2.setCursorVisible(false);
+                    mText0.requestFocus();
+                    mText0.setSelected(true);
+                    mText0.setSelection(0);
+                } else if (mText1.getText().toString().equals("")) {
+                    mText2.setSelected(false);
+                    mText2.setCursorVisible(false);
                     mText1.requestFocus();
                     mText1.setSelected(true);
                     mText1.setSelection(0);
-                }else if (mText2.getText().toString().equals("")){
-                    mText3.setSelected(false);
-                    mText3.setCursorVisible(false);
-                    mText2.requestFocus();
-                    mText2.setSelected(true);
-                    mText2.setSelection(0);
                 }
-                
+
             }
         });
-        
+
+
         /*------------------------------------------------
          *  FOURTH BOX
          -------------------------------------------------*/
-        mText4.addTextChangedListener(new TextWatcher() {
+        mText3.addTextChangedListener(new TextWatcher() {
 
             @Override
             public void onTextChanged(CharSequence s, int start, int before,
-                    int count) {
+                                      int count) {
             }
 
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,
-                    int after) {
+                                          int after) {
             }
 
             @Override
             public void afterTextChanged(Editable s) {
                 if (s.length() > 0) {
-                    
-                    if (!mConfirmingPassCode){
-                       mTempText[3] = mText4.getText().toString();
-                    }
-                    mText1.requestFocus();
 
-                    if (!mPassCodeChecked){
-                        mPassCodeChecked = checkPassCode();
+                    if (!mConfirmingPassCode) {
+                        mPassCodeDigits[3] = mText3.getText().toString();
                     }
-                    
-                    if (mPassCodeChecked) {
-                        if (ACTION_REQUEST.equals(getIntent().getAction())) {
-                            finish();
-
-                        } else if (mPassCodeChecked) {
-                            String newState = getIntent().getStringExtra(EXTRA_NEW_STATE);
-
-                            // TODO - next decision should done according to the current state of PIN in prefs (enable or not), not whatever says de client
-                            if (newState.equals("false")) {
-                                SharedPreferences.Editor appPrefs = PreferenceManager
-                                        .getDefaultSharedPreferences(getApplicationContext()).edit();
-                                appPrefs.putBoolean("set_pincode", false);
-                                appPrefs.commit();
-
-                                setInitVars();
-                                passCodeEnd(false);
+                    mText0.requestFocus();
 
-                            } else {
+                    processFullPassCode();
 
-                                if (!mConfirmingPassCode) {
-                                    passCodeChangeRequest();
-
-                                } else {
-                                    confirmPassCode();
-                                }
-                            }
-
-                        }
-                    }    
+                } else {
+                    Log_OC.w(TAG, "Input in text box 3 resulted in empty string");
                 }
             }
         });
 
-        
-        
-        mText4.setOnKeyListener(new OnKeyListener() {
+
+        mText3.setOnKeyListener(new OnKeyListener() {
 
             @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (keyCode == KeyEvent.KEYCODE_DEL && mBChange) {
-                    mText3.requestFocus();
+                    mText2.requestFocus();
                     if (!mConfirmingPassCode)
-                        mTempText[2]="";
-                    mText3.setText("");
-                    mBChange= false;
-                    
-                }else if(!mBChange){
-                    mBChange=true;    
+                        mPassCodeDigits[2] = "";
+                    mText2.setText("");
+                    mBChange = false;
+
+                } else if (!mBChange) {
+                    mBChange = true;
                 }
                 return false;
             }
         });
-        
-       mText4.setOnFocusChangeListener(new OnFocusChangeListener() {
-            
+
+        mText3.setOnFocusChangeListener(new OnFocusChangeListener() {
+
             @Override
             public void onFocusChange(View v, boolean hasFocus) {
-                mText4.setCursorVisible(true);
-                
-                if (mText1.getText().toString().equals("")){
-                    mText4.setSelected(false);
-                    mText4.setCursorVisible(false);
+                mText3.setCursorVisible(true);
+
+                if (mText0.getText().toString().equals("")) {
+                    mText3.setSelected(false);
+                    mText3.setCursorVisible(false);
+                    mText0.requestFocus();
+                    mText0.setSelected(true);
+                    mText0.setSelection(0);
+                } else if (mText1.getText().toString().equals("")) {
+                    mText3.setSelected(false);
+                    mText3.setCursorVisible(false);
                     mText1.requestFocus();
                     mText1.setSelected(true);
                     mText1.setSelection(0);
-                }else if (mText2.getText().toString().equals("")){
-                    mText4.setSelected(false);
-                    mText4.setCursorVisible(false);
+                } else if (mText2.getText().toString().equals("")) {
+                    mText3.setSelected(false);
+                    mText3.setCursorVisible(false);
                     mText2.requestFocus();
                     mText2.setSelected(true);
                     mText2.setSelection(0);
-                }else if (mText3.getText().toString().equals("")){
-                    mText4.setSelected(false);
-                    mText4.setCursorVisible(false);
-                    mText3.requestFocus();
-                    mText3.setSelected(true);
-                    mText3.setSelection(0);
                 }
-                
+
             }
         });
         
         
         
     } // end setTextListener
+
+
+    /**
+     * Processes the pass code entered by the user just after the last digit was in.
+     *
+     * Takes into account the action requested to the activity, the currently saved pass code and the previously
+     * typed pass code, if any.
+     */
+    private void processFullPassCode() {
+        if (ACTION_REQUEST.equals(getIntent().getAction())) {
+            if (checkPassCode()) {
+                /// pass code accepted in request, user is allowed to access the app
+                finish();
+
+            }  else {
+                showErrorAndRestart(R.string.common_error, R.string.pass_code_enter_pass_code, View.INVISIBLE);
+                    /// TODO better error message
+            }
+
+        } else if (ACTION_DISABLE.equals(getIntent().getAction())) {
+            if (checkPassCode()) {
+                /// pass code accepted when disabling, pass code is removed
+                SharedPreferences.Editor appPrefs = PreferenceManager
+                        .getDefaultSharedPreferences(getApplicationContext()).edit();
+                appPrefs.putBoolean("set_pincode", false);  // TODO remove; this should be unnecessary, was done before entering in the activity
+                appPrefs.commit();
+
+                Toast.makeText(PassCodeActivity.this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
+                finish();
+
+            } else {
+                showErrorAndRestart(R.string.common_error, R.string.pass_code_enter_pass_code, View.INVISIBLE);
+                    /// TODO better error message
+            }
+
+        } else if (ACTION_ENABLE.equals(getIntent().getAction())) {
+            /// enabling pass code
+            if (!mConfirmingPassCode) {
+                requestPassCodeConfirmation();
+
+            } else if (confirmPassCode()) {
+                /// confirmed: user typed the same pass code twice
+                savePassCodeAndExit();
+
+            } else {
+                showErrorAndRestart(
+                        R.string.pass_code_mismatch, R.string.pass_code_configure_your_pass_code, View.VISIBLE
+                );
+            }
+        }
+    }
+
     
-    
-    protected void passCodeChangeRequest(){
-    
-        clearBoxes(); 
+    private void showErrorAndRestart(int errorMessage, int headerMessage, int explanationVisibility) {
+        Arrays.fill(mPassCodeDigits, null);
+        CharSequence errorSeq = getString(errorMessage);
+        Toast.makeText(this, errorSeq, Toast.LENGTH_LONG).show();
+        mPassCodeHdr.setText(headerMessage);                // TODO check if really needed
+        mPassCodeHdrExplanation.setVisibility(explanationVisibility);  // TODO check if really needed
+        clearBoxes();
+    }
+
+
+    /**
+     * Ask to the user for retyping the pass code just entered before saving it as the current pass code.
+     */
+    protected void requestPassCodeConfirmation(){
+        clearBoxes();
         mPassCodeHdr.setText(R.string.pass_code_reenter_your_pass_code);
         mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
-        mConfirmingPassCode =true;
-        
+        mConfirmingPassCode = true;
     }
-    
-    
+
+    /**
+     * Compares pass code entered by the user with the value currently saved in the app.
+     *
+     * @return     'True' if entered pass code equals to the saved one.
+     */
     protected boolean checkPassCode(){
-        
-        
         SharedPreferences appPrefs = PreferenceManager
-                .getDefaultSharedPreferences(getApplicationContext());
-        
-       String pText1 = appPrefs.getString("PrefPinCode1", null);
-        String pText2 = appPrefs.getString("PrefPinCode2", null);
-        String pText3 = appPrefs.getString("PrefPinCode3", null);
-        String pText4 = appPrefs.getString("PrefPinCode4", null);
-
-        if ( mTempText[0].equals(pText1) && 
-             mTempText[1].equals(pText2) &&
-             mTempText[2].equals(pText3) &&
-             mTempText[3].equals(pText4) ) {
-            
-            return true;
-        
-        
-        }else {
-            Arrays.fill(mTempText, null);
-            CharSequence errorSeq = getString(R.string.common_error);
-            Toast.makeText(this, errorSeq, Toast.LENGTH_LONG).show();
-            
-            clearBoxes(); 
-            mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);
-            mPassCodeHdrExplanation.setVisibility(View.INVISIBLE);
-            mNewPasswordEntered = true;
-            mConfirmingPassCode = false;
-            
-        }
-     
-        
-        return false;
+            .getDefaultSharedPreferences(getApplicationContext());
+
+        String savedPassCodeDigits[] = new String[4];
+        savedPassCodeDigits[0] = appPrefs.getString("PrefPinCode1", null);
+        savedPassCodeDigits[1] = appPrefs.getString("PrefPinCode2", null);
+        savedPassCodeDigits[2] = appPrefs.getString("PrefPinCode3", null);
+        savedPassCodeDigits[3] = appPrefs.getString("PrefPinCode4", null);
+
+        return (
+            mPassCodeDigits[0].equals(savedPassCodeDigits[0]) &&
+            mPassCodeDigits[1].equals(savedPassCodeDigits[0]) &&
+            mPassCodeDigits[2].equals(savedPassCodeDigits[0]) &&
+            mPassCodeDigits[3].equals(savedPassCodeDigits[0])
+        );
     }
-    
-    protected void confirmPassCode(){
-        
+
+    /**
+     * Compares pass code retyped by the user with the value entered just before.
+     *
+     * @return     'True' if retyped pass code equals to the entered before.
+     */
+    protected boolean confirmPassCode(){
         mConfirmingPassCode = false;
-        
-        String rText1 = mText1.getText().toString();
-        String rText2 = mText2.getText().toString();
-        String rText3 = mText3.getText().toString();
-        String rText4 = mText4.getText().toString();
-        
-        if ( mTempText[0].equals(rText1) && 
-             mTempText[1].equals(rText2) &&
-             mTempText[2].equals(rText3) &&
-             mTempText[3].equals(rText4) ) {
-                        
-            savePassCodeAndExit();
-            
-        } else {
-            Arrays.fill(mTempText, null);
-            CharSequence cseq = getString(R.string.pass_code_mismatch);
-            Toast.makeText(this, cseq, Toast.LENGTH_LONG).show();
-            
-            mPassCodeHdr.setText(R.string.pass_code_configure_your_pass_code);
-            mPassCodeHdrExplanation.setVisibility(View.VISIBLE);
-            clearBoxes();
-        }
-    
-    }
-   
-    
-    protected void passCodeEnd(boolean state){
-        CharSequence cseq;
-        if (state){
-           cseq = getString(R.string.pass_code_stored);
-        }else{
-             cseq = getString(R.string.pass_code_removed);
-        }
-        
-        Toast.makeText(this, cseq, Toast.LENGTH_LONG).show();
-        finish();
-    }
-    
-    protected void savePassCodeAndExit(){
-        SharedPreferences.Editor appPrefs = PreferenceManager
-                .getDefaultSharedPreferences(getApplicationContext()).edit();
-        
-        appPrefs.putString("PrefPinCode1", mTempText[0]);
-        appPrefs.putString("PrefPinCode2",mTempText[1]);
-        appPrefs.putString("PrefPinCode3", mTempText[2]);
-        appPrefs.putString("PrefPinCode4", mTempText[3]);
-        appPrefs.putBoolean("set_pincode",true);
-        appPrefs.commit();
-        
-        passCodeEnd(true);
+
+        String retypedPassCodeDigits[] = new String[4];
+        retypedPassCodeDigits[0] = mText0.getText().toString();
+        retypedPassCodeDigits[1] = mText1.getText().toString();
+        retypedPassCodeDigits[2] = mText2.getText().toString();
+        retypedPassCodeDigits[3] = mText3.getText().toString();
+
+        return (
+            mPassCodeDigits[0].equals(retypedPassCodeDigits[0]) &&
+            mPassCodeDigits[1].equals(retypedPassCodeDigits[0]) &&
+            mPassCodeDigits[2].equals(retypedPassCodeDigits[0]) &&
+            mPassCodeDigits[3].equals(retypedPassCodeDigits[0])
+        );
     }
-    
-    
+
+    /**
+     * Sets the input fields to empty strings and puts the focus on the first one.
+     */
     protected void clearBoxes(){
+        mText0.setText("");
         mText1.setText("");
         mText2.setText("");
         mText3.setText("");
-        mText4.setText("");
-        mText1.requestFocus(); 
+        mText0.requestFocus();
     }
-    
-    
+
+    /**
+     * Overrides click on the BACK arrow to correctly cancel ACTION_ENABLE or ACTION_DISABLE, while preventing
+     * than ACTION_REQUEST may be worked around.
+     *
+     * @param keyCode       Key code of the key that triggered the down event.
+     * @param event         Event triggered.
+     * @return              'True' when the key event was processed by this method.
+     */
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event){
         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
-            if (ACTION_TOGGLE.equals(getIntent().getAction())){
-                SharedPreferences.Editor appPrefsE = PreferenceManager
-                    .getDefaultSharedPreferences(getApplicationContext()).edit();
-            
-                SharedPreferences appPrefs = PreferenceManager
-                    .getDefaultSharedPreferences(getApplicationContext());
-            
-                boolean state = appPrefs.getBoolean("set_pincode", false);
-                appPrefsE.putBoolean("set_pincode",!state); 
-                appPrefsE.commit();
-                setInitVars();
-                finish();
+            if (ACTION_ENABLE.equals(getIntent().getAction()) || ACTION_DISABLE.equals(getIntent().getAction())) {
+                revertActionAndExit();
             }
-            return true; 
+            return true;
         }
         return super.onKeyDown(keyCode, event);
-    }     
+    }
+
+    /**
+     * Saves the pass code input by the user as the current pass code.
+     */
+    protected void savePassCodeAndExit() {
+        SharedPreferences.Editor appPrefs = PreferenceManager
+                .getDefaultSharedPreferences(getApplicationContext()).edit();
+        
+        appPrefs.putString("PrefPinCode1", mPassCodeDigits[0]);
+        appPrefs.putString("PrefPinCode2", mPassCodeDigits[1]);
+        appPrefs.putString("PrefPinCode3", mPassCodeDigits[2]);
+        appPrefs.putString("PrefPinCode4", mPassCodeDigits[3]);
+        appPrefs.putBoolean("set_pincode", true);    /// TODO remove; unnecessary, Preferences did it before entering here
+        appPrefs.commit();
+
+        Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
+        finish();
+    }
+
+    /**
+     * Cancellation of ACTION_ENABLE or ACTION_DISABLE; reverts the enable or disable action done by
+     * {@link Preferences}, then finishes.
+     */
+    protected void revertActionAndExit() {
+        SharedPreferences.Editor appPrefsE = PreferenceManager
+                .getDefaultSharedPreferences(getApplicationContext()).edit();
+
+        SharedPreferences appPrefs = PreferenceManager
+                .getDefaultSharedPreferences(getApplicationContext());
+
+        boolean state = appPrefs.getBoolean("set_pincode", false);
+        appPrefsE.putBoolean("set_pincode", !state);
+        // TODO WIP: this is reverting the value of the preference because it was changed BEFORE entering
+        // TODO         in this activity; was the PreferenceCheckBox in the caller who did it
+        appPrefsE.commit();
+        finish();
+    }
+
 }

+ 4 - 2
src/com/owncloud/android/ui/activity/Preferences.java

@@ -167,8 +167,10 @@ public class Preferences extends SherlockPreferenceActivity
                 @Override
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
                     Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
-                    i.setAction(PassCodeActivity.ACTION_TOGGLE);
-                    i.putExtra(PassCodeActivity.EXTRA_NEW_STATE, newValue.toString());
+                    Boolean enable = (Boolean) newValue;
+                    i.setAction(
+                            enable.booleanValue() ? PassCodeActivity.ACTION_ENABLE : PassCodeActivity.ACTION_DISABLE
+                    );
                     startActivity(i);
                     
                     return true;