More fixes to PIN code
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / PinCodeActivity.java
index c620952..d9ae73c 100644 (file)
@@ -42,6 +42,8 @@ import android.widget.TextView;
 public class PinCodeActivity extends SherlockFragmentActivity {
 
   
+    public final static String EXTRA_ACTIVITY = "eu.alefzero.owncloud.ui.activity.PinCodeActivity.ACTIVITY";
+    public final static String EXTRA_NEW_STATE = "eu.alefzero.owncloud.ui.activity.PinCodeActivity.NEW_STATE";
     
     Button bCancel;
     TextView mPinHdr;
@@ -58,7 +60,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) {
@@ -66,12 +68,13 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         setContentView(R.layout.pincodelock); 
         
         Intent intent = getIntent();
-        activity = intent.getStringExtra("activity");
+        activity = intent.getStringExtra(EXTRA_ACTIVITY);
      
         bCancel = (Button) findViewById(R.id.cancel);
         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);
@@ -80,44 +83,85 @@ 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);
         bCancel.setVisibility(View.GONE);
-        mPinHdr.setText("Please, Insert your PIN");
+        mPinHdr.setText(R.string.pincode_enter_pin_code);
     }
     
-    
-    
-    protected void setChangePincodeView(){
-        mPinHdr.setText("Configure your PIN");
+   
+    protected void setChangePincodeView(boolean state){
+       
+        if(state){
+        bCancel.setVisibility(View.VISIBLE);
         bCancel.setOnClickListener(new OnClickListener() {
-
-            @Override
-            public void onClick(View v) {
-                // TODO Auto-generated method stub
-                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();
             }
         });
+        }  
     
     }
     
+    
+    
     /*
      *  
      */
@@ -296,7 +340,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
             @Override
             public void onTextChanged(CharSequence s, int start, int before,
                     int count) {
-                // TODO Auto-generated method stub
+                
                 if (s.length() > 0) {
                    
                    if (!confirmingPinCode){
@@ -308,12 +352,12 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                        pinCodeChecked = checkPincode();
                    }
                    
-                   if (pinCodeChecked && activity.equals("splash")){
+                   if (pinCodeChecked && activity.equals("FileDisplayActivity")){
                        finish();
                    } else if (pinCodeChecked){
                        
                        Intent intent = getIntent();
-                       String newState = intent.getStringExtra("pinNewState");
+                       String newState = intent.getStringExtra(EXTRA_NEW_STATE);
                        
                        if (newState.equals("false")){
                            SharedPreferences.Editor appPrefs = PreferenceManager
@@ -321,20 +365,18 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                            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){
                                pinCodeChangeRequest();
-                           }else if (newPasswordEntered && !confirmingPinCode){
-                               mPinHdr.setText("Confirm your PINCode, please");
+                           } else if (newPasswordEntered && !confirmingPinCode){
+                               mPinHdr.setText(R.string.pincode_reenter_your_pincode);
                                confirmingPinCode = true;
                                clearBoxes();
-                           }else {
+                           } else {
                                confirmPincode();
                            }
                        }
@@ -403,37 +445,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("Please, insert your 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;
         
     }
     
@@ -460,7 +474,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         }else {
             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
@@ -472,7 +487,7 @@ public class PinCodeActivity extends SherlockFragmentActivity {
             });
             aDialog.show();
             clearBoxes(); 
-            mPinHdr.setText("Configure your PIN");
+            mPinHdr.setText(R.string.pincode_enter_pin_code);
             newPasswordEntered = true;
             confirmingPinCode = false;
             
@@ -502,7 +517,8 @@ public class PinCodeActivity extends SherlockFragmentActivity {
             
             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
@@ -513,12 +529,37 @@ public class PinCodeActivity extends SherlockFragmentActivity {
                 
             });
             aDialog.show();
-            mPinHdr.setText("Configure your PIN");
+            mPinHdr.setText(R.string.pincode_configure_your_pin);
             clearBoxes();
         }
     
     }
    
+    
+    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();
@@ -530,7 +571,10 @@ public class PinCodeActivity extends SherlockFragmentActivity {
         appPrefs.putBoolean("set_pincode",true);
         appPrefs.commit();
         
-        finish();
+        pinCodeEnd(true);
+        
+        
+        
     }
     
     
@@ -542,6 +586,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);
+    }
+    
             
             
 }