OCFile stores again decoded remote paths; WebdavUtils.encode(...) added; fixed space...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / PinCodeActivity.java
index 485cb67..6c65bbb 100644 (file)
@@ -17,6 +17,8 @@
  */
 package eu.alefzero.owncloud.ui.activity;
 
+import java.util.Arrays;
+
 import com.actionbarsherlock.app.SherlockFragmentActivity;
 
 import eu.alefzero.owncloud.R;
@@ -60,7 +62,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
     boolean pinCodeChecked = false;
     boolean newPasswordEntered = false;
     boolean bChange = true; // to control that only one blocks jump
-    int tCounter ; // Count the number of attempts an user could introduce de PIN code
+    int tCounter ; // Count the number of attempts an user could introduce the PIN code
 
     
     protected void onCreate(Bundle savedInstanceState) {
@@ -74,6 +76,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         mPinHdr = (TextView) findViewById(R.id.pinHdr);
         mText1 = (EditText) findViewById(R.id.txt1);
         mText1.requestFocus();
+        getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);        
         mText2 = (EditText) findViewById(R.id.txt2);
         mText3 = (EditText) findViewById(R.id.txt3);
         mText4 = (EditText) findViewById(R.id.txt4);
@@ -82,22 +85,50 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         SharedPreferences appPrefs = PreferenceManager
                 .getDefaultSharedPreferences(getApplicationContext());
         
-        // Not PIN Code defined yet
-        if ( appPrefs.getString("PrefPinCode1", null) == null ){
-            setChangePincodeView();
+        // Not PIN Code defined yet.
+        // In a previous version settings is allow from start
+        if ( (appPrefs.getString("PrefPinCode1", null) == null ) ){
+            setInitView();
             pinCodeChecked = true; 
             newPasswordEntered = true;
             
-        } else {
-            setInitView(); 
-        }
+        }else{ 
+            
+            if (appPrefs.getBoolean("set_pincode", false)){
+               // pincode activated
+               if (activity.equals("preferences")){
+                // PIN has been activated yet
+                 mPinHdr.setText(R.string.pincode_configure_your_pin);
+                 pinCodeChecked = true ; // No need to check it 
+                 setChangePincodeView(true);
+               }else{
+                // PIN active
+                 bCancel.setVisibility(View.INVISIBLE);
+                 bCancel.setVisibility(View.GONE);
+                 mPinHdr.setText(R.string.pincode_enter_pin_code);
+                 setChangePincodeView(false);
+              }
+            
+           }else {
+            // pincode removal
+              mPinHdr.setText(R.string.pincode_enter_pin_code);
+              pinCodeChecked = false;
+              setChangePincodeView(true); 
+           }
            
-       
+        }
         setTextListeners();
         
         
     }
-       
+     
+    protected void setInitVars(){
+        confirmingPinCode = false;
+        pinCodeChecked = false;
+        newPasswordEntered = false;
+
+    }
     
     protected void setInitView(){
         bCancel.setVisibility(View.INVISIBLE);
@@ -105,20 +136,34 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         mPinHdr.setText(R.string.pincode_enter_pin_code);
     }
     
-    
-    
-    protected void setChangePincodeView(){
-        mPinHdr.setText(R.string.pincode_configure_your_pin);
+   
+    protected void setChangePincodeView(boolean state){
+       
+        if(state){
+        bCancel.setVisibility(View.VISIBLE);
         bCancel.setOnClickListener(new OnClickListener() {
-
-            @Override
-            public void onClick(View v) {
-                finish();
+        @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();
             }
         });
+        }  
     
     }
     
+    
+    
     /*
      *  
      */
@@ -139,7 +184,9 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                       tempText[0] = mText1.getText().toString();
                       
                    }
+                                      
                    mText2.requestFocus();
+                   
                 }
             }
 
@@ -172,7 +219,9 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                     if (!confirmingPinCode){
                         tempText[1] = mText2.getText().toString();
                     }
+                    
                     mText3.requestFocus();
+                    
                 }
             }
 
@@ -200,7 +249,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
 
                     mText1.setText("");
                     mText1.requestFocus();
-                    tempText[0] = "";
+                    if (!confirmingPinCode)
+                       tempText[0] = "";
                     bChange= false;
                 
                 }else if(!bChange){
@@ -218,6 +268,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                 // TODO Auto-generated method stub
                 if (mText1.getText().toString().equals("")){
                     mText1.requestFocus(); 
+                }else {
+                    mText1.append("");
                 }
                 
             }
@@ -238,6 +290,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                         tempText[2] = mText3.getText().toString();
                     }
                     mText4.requestFocus();
+                    
                 }
             }
 
@@ -263,7 +316,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
 
                 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
                     mText2.requestFocus();
-                    tempText[1] = "";
+                    if (!confirmingPinCode)
+                        tempText[1] = "";
                     mText2.setText("");
                     bChange= false;
                     
@@ -321,18 +375,15 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                                    .getDefaultSharedPreferences(getApplicationContext()).edit();
                            appPrefs.putBoolean("set_pincode",false);
                            appPrefs.commit();
-                           // TODO Alert Message que salte y vuelva a la pantalla anterior
                            
-                           finish();
+                           setInitVars();
+                           pinCodeEnd(false);
                            
                        }else{
                        
-                           if (!confirmingPinCode && !newPasswordEntered){
+                           if (!confirmingPinCode){
                                pinCodeChangeRequest();
-                           } else if (newPasswordEntered && !confirmingPinCode){
-                               mPinHdr.setText(R.string.pincode_confirm_your_pincode);
-                               confirmingPinCode = true;
-                               clearBoxes();
+                            
                            } else {
                                confirmPincode();
                            }
@@ -368,7 +419,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
 
                 if (keyCode == KeyEvent.KEYCODE_DEL && bChange) {
                     mText3.requestFocus();
-                    tempText[2]="";
+                    if (!confirmingPinCode)
+                        tempText[2]="";
                     mText3.setText("");
                     bChange= false;
                     
@@ -402,37 +454,9 @@ public class PinCodeActivity extends SherlockFragmentActivity {
     
     protected void pinCodeChangeRequest(){
     
-        AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);
-        aBuilder.setMessage("Do yo want to set a new PIN Code")
-              .setCancelable(false)
-              .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
-                 
-                 @Override
-                 public void onClick(DialogInterface dialog, int which) {
-                     // TODO Auto-generated method stub
-                     setChangePincodeView();
-                     mPinHdr.setText(R.string.pincode_enter_new_pin_code);
-                     clearBoxes();
-                     newPasswordEntered = true;
-                 }
-             })
-             .setNegativeButton("No", new DialogInterface.OnClickListener() {
-                 
-                 @Override
-                 public void onClick(DialogInterface dialog, int which) {
-                     // TODO Auto-generated method stub
-                     SharedPreferences.Editor appPrefs = PreferenceManager
-                             .getDefaultSharedPreferences(getApplicationContext()).edit();
-                     appPrefs.putBoolean("set_pincode",false);
-                     appPrefs.commit();
-                     finish();
-                 }
-             });
-        
-        AlertDialog alert =aBuilder.create();
-      
-        alert.show();
-        
+        clearBoxes(); 
+        mPinHdr.setText(R.string.pincode_reenter_your_pincode); 
+        confirmingPinCode =true;
         
     }
     
@@ -457,9 +481,11 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         
         
         }else {
+            Arrays.fill(tempText, null);
             AlertDialog aDialog = new AlertDialog.Builder(this).create();
             aDialog.setTitle("ERROR");
-            aDialog.setMessage("Wrong PIN");
+            CharSequence cseq = getString(R.string.pincode_wrong);
+            aDialog.setMessage(cseq);
             aDialog.setButton("OK", new DialogInterface.OnClickListener(){
 
                 @Override
@@ -471,7 +497,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
             });
             aDialog.show();
             clearBoxes(); 
-            mPinHdr.setText(R.string.pincode_configure_your_pin);
+            mPinHdr.setText(R.string.pincode_enter_pin_code);
             newPasswordEntered = true;
             confirmingPinCode = false;
             
@@ -499,9 +525,11 @@ public class PinCodeActivity extends SherlockFragmentActivity {
             
         } else {
             
+            Arrays.fill(tempText, null);
             AlertDialog aDialog = new AlertDialog.Builder(this).create();
             aDialog.setTitle("ERROR");
-            aDialog.setMessage("PIN Code Mismatch");
+            CharSequence cseq = getString(R.string.pincode_mismatch);
+            aDialog.setMessage(cseq);
             aDialog.setButton("OK", new DialogInterface.OnClickListener(){
 
                 @Override
@@ -518,6 +546,31 @@ public class PinCodeActivity extends SherlockFragmentActivity {
     
     }
    
+    
+    protected void pinCodeEnd(boolean state){
+        AlertDialog aDialog = new AlertDialog.Builder(this).create();
+        
+        if (state){
+            aDialog.setTitle("SAVE & EXIT");
+            aDialog.setMessage("PIN Code Activated");
+        }else{
+            aDialog.setTitle("SAVE & EXIT");
+            aDialog.setMessage("PIN Code Removed"); 
+        }
+        
+        aDialog.setButton("OK", new DialogInterface.OnClickListener(){
+
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                // TODO Auto-generated method stub("");
+                finish();
+                return; 
+            }
+            
+        });
+        aDialog.show(); 
+    }
+    
     protected void savePincodeAndExit(){
         SharedPreferences.Editor appPrefs = PreferenceManager
                 .getDefaultSharedPreferences(getApplicationContext()).edit();
@@ -529,7 +582,10 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         appPrefs.putBoolean("set_pincode",true);
         appPrefs.commit();
         
-        finish();
+        pinCodeEnd(true);
+        
+        
+        
     }
     
     
@@ -541,6 +597,33 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         mText4.setText("");
         mText1.requestFocus(); 
     }
+    
+    
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event){
+        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()== 0){
+            
+            if (activity.equals("preferences")){
+                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();
+            }
+            return true; 
+            
+        }
+        
+        return super.onKeyDown(keyCode, event);
+    }
+    
             
             
 }