Clean up on SecureSipCall

Refs #41441
diff --git a/src/org/sflphone/fragments/CallFragment.java b/src/org/sflphone/fragments/CallFragment.java
index 69b4c24..28b9618 100644
--- a/src/org/sflphone/fragments/CallFragment.java
+++ b/src/org/sflphone/fragments/CallFragment.java
@@ -263,34 +263,33 @@
     public void secureZrtpOn(Conference updated, String id) {
         Log.i(TAG, "secureZrtpOn");
         mCallbacks.updateDisplayedConference(updated);
+        updateSecurityDisplay();
     }
 
     @Override
     public void secureZrtpOff(Conference updated, String id) {
         Log.i(TAG, "secureZrtpOff");
         mCallbacks.updateDisplayedConference(updated);
+        updateSecurityDisplay();
     }
 
     @Override
     public void displaySAS(Conference updated, final String securedCallID) {
         Log.i(TAG, "displaySAS");
         mCallbacks.updateDisplayedConference(updated);
-        SecureSipCall display = (SecureSipCall) getConference().getCallById(securedCallID);
-        enableZRTP(display);
+        updateSecurityDisplay();
     }
 
     @Override
     public void zrtpNegotiationFailed(Conference c, String securedCallID) {
         mCallbacks.updateDisplayedConference(c);
-        SecureSipCall display = (SecureSipCall) getConference().getCallById(securedCallID);
-        enableZRTP(display);
+        updateSecurityDisplay();
     }
 
     @Override
     public void zrtpNotSupported(Conference c, String securedCallID) {
         mCallbacks.updateDisplayedConference(c);
-        SecureSipCall display = (SecureSipCall) getConference().getCallById(securedCallID);
-        enableZRTP(display);
+        updateSecurityDisplay();
     }
 
     @Override
@@ -392,46 +391,60 @@
             dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
             dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
             getBubbleFor(partee, (int) (mBubbleModel.width / 2 + dX), (int) (mBubbleModel.height / 2 + dY));
-            if (partee instanceof SecureSipCall)
-                enableZRTP((SecureSipCall) partee);
         }
+        updateSecurityDisplay();
         mBubbleModel.clearAttractors();
     }
 
-    private void enableZRTP(final SecureSipCall secured) {
-        Log.i(TAG, "enable ZRTP");
-        switch (secured.displayModule()) {
-            case SecureSipCall.DISPLAY_GREEN_LOCK:
-                showLock(R.drawable.green_lock);
-                break;
-            case SecureSipCall.DISPLAY_RED_LOCK:
-                showLock(R.drawable.red_lock);
-                break;
-            case SecureSipCall.DISPLAY_CONFIRM_SAS:
-                final Button sas = (Button) mSecuritySwitch.findViewById(R.id.confirm_sas);
-                sas.setText("Confirm SAS: " + secured.getSAS());
-                sas.setOnClickListener(new View.OnClickListener() {
-                    @Override
-                    public void onClick(View v) {
-                        try {
-                            mCallbacks.getService().confirmSAS(secured.getCallId());
-                            showLock(R.drawable.green_lock);
-                        } catch (RemoteException e) {
-                            e.printStackTrace();
+    private void updateSecurityDisplay() {
+
+        //First we check if at least one participan use a security layer.
+        if (!getConference().useSecureLayer())
+            return;
+
+        Log.i(TAG, "Enable security display");
+        if (getConference().hasMultipleParticipants()) {
+            //TODO What layout should we put?
+        } else {
+            final SecureSipCall secured = (SecureSipCall) getConference().getParticipants().get(0);
+            switch (secured.displayModule()) {
+                case SecureSipCall.DISPLAY_GREEN_LOCK:
+                    Log.i(TAG, "DISPLAY_GREEN_LOCK");
+                    showLock(R.drawable.green_lock);
+                    break;
+                case SecureSipCall.DISPLAY_RED_LOCK:
+                    Log.i(TAG, "DISPLAY_RED_LOCK");
+                    showLock(R.drawable.red_lock);
+                    break;
+                case SecureSipCall.DISPLAY_CONFIRM_SAS:
+                    final Button sas = (Button) mSecuritySwitch.findViewById(R.id.confirm_sas);
+                    Log.i(TAG, "Confirm SAS: " + secured.getSAS());
+                    sas.setText("Confirm SAS: " + secured.getSAS());
+                    sas.setOnClickListener(new View.OnClickListener() {
+                        @Override
+                        public void onClick(View v) {
+                            try {
+                                mCallbacks.getService().confirmSAS(secured.getCallId());
+                                showLock(R.drawable.green_lock);
+                            } catch (RemoteException e) {
+                                e.printStackTrace();
+                            }
                         }
-                    }
-                });
-                mSecuritySwitch.setVisibility(View.VISIBLE);
-                break;
-            case SecureSipCall.DISPLAY_NONE:
-                break;
+                    });
+                    mSecuritySwitch.setDisplayedChild(0);
+                    mSecuritySwitch.setVisibility(View.VISIBLE);
+                    break;
+                case SecureSipCall.DISPLAY_NONE:
+                    break;
+            }
         }
     }
 
     private void showLock(int resId) {
         ImageView lock = (ImageView) mSecuritySwitch.findViewById(R.id.lock_image);
         lock.setImageDrawable(getResources().getDrawable(resId));
-        mSecuritySwitch.showNext();
+        Log.i(TAG, "mSecuritySwitch.getDisplayedChild(): " + mSecuritySwitch.getDisplayedChild());
+        mSecuritySwitch.setDisplayedChild(1);
         mSecuritySwitch.setVisibility(View.VISIBLE);
     }
 
diff --git a/src/org/sflphone/model/Conference.java b/src/org/sflphone/model/Conference.java
index 7c8c644..57581ba 100644
--- a/src/org/sflphone/model/Conference.java
+++ b/src/org/sflphone/model/Conference.java
@@ -54,6 +54,14 @@
         participants.remove(toRemove);
     }
 
+    public boolean useSecureLayer() {
+        for(SipCall call : participants){
+            if(call.getAccount().useSecureLayer())
+                return true;
+        }
+        return false;
+    }
+
     public interface state {
         int ACTIVE_ATTACHED = 0;
         int ACTIVE_DETACHED = 1;
diff --git a/src/org/sflphone/model/SecureSipCall.java b/src/org/sflphone/model/SecureSipCall.java
index 9932f98..82feaca 100644
--- a/src/org/sflphone/model/SecureSipCall.java
+++ b/src/org/sflphone/model/SecureSipCall.java
@@ -34,11 +34,7 @@
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.os.RemoteException;
 import android.util.Log;
-import android.view.View;
-import android.widget.Button;
-import org.sflphone.R;
 
 
 public class SecureSipCall extends SipCall {
@@ -52,17 +48,9 @@
     public final static int DISPLAY_CONFIRM_SAS = 2;
     public final static int DISPLAY_NONE = 3;
 
-    private boolean sdesIsOn;
-    private boolean rtpFallback;
-    /*
-    *
-    srtp:
-    enable: false
-    keyExchange: sdes
-    rtpFallback: false
-    stunEnabled: false
-    stunServer:
 
+    private boolean sdesIsOn;
+/*
     tls:
     calist:
     certificate:
@@ -77,26 +65,12 @@
     tlsPort: 5061
     verifyClient: true
     verifyServer: true
-
-    zrtp:
-    displaySas: true
-    displaySasOnce: false
-    helloHashEnabled: true
-    notSuppWarning: true
-
-    *
-    *
-    *
-    * */
+*/
 
     private String SAS;
     private boolean needSASConfirmation;
 
-    public boolean supportZRTP() {
-        return !zrtpNotSupported;
-    }
-
-    private boolean zrtpNotSupported;
+    private boolean zrtpIsSupported;
 
     // static preferences of account
     private final boolean displaySas;
@@ -110,10 +84,11 @@
         super(call);
         isInitialized = false;
         displaySas = secure.getBoolean(SecureSipCall.DISPLAY_SAS, false);
-        needSASConfirmation = false;
+        needSASConfirmation = displaySas;
+        Log.i("SecureSipCall", "needSASConfirmation " + needSASConfirmation);
         alertIfZrtpNotSupported = secure.getBoolean(SecureSipCall.DISPLAY_WARNING_ZRTP_NOT_SUPPORTED, false);
         displaySASOnHold = secure.getBoolean(SecureSipCall.DISPLAY_SAS_ONCE, false);
-        zrtpNotSupported = false;
+        zrtpIsSupported = false;
         sdesIsOn = false;
     }
 
@@ -136,7 +111,7 @@
         isInitialized = in.readByte() == 1;
         alertIfZrtpNotSupported = in.readByte() == 1;
         displaySASOnHold = in.readByte() == 1;
-        zrtpNotSupported = in.readByte() == 1;
+        zrtpIsSupported = in.readByte() == 1;
         needSASConfirmation = in.readByte() == 1;
         sdesIsOn = in.readByte() == 1;
     }
@@ -149,7 +124,7 @@
         out.writeByte((byte) (isInitialized ? 1 : 0));
         out.writeByte((byte) (alertIfZrtpNotSupported ? 1 : 0));
         out.writeByte((byte) (displaySASOnHold ? 1 : 0));
-        out.writeByte((byte) (zrtpNotSupported ? 1 : 0));
+        out.writeByte((byte) (zrtpIsSupported ? 1 : 0));
         out.writeByte((byte) (needSASConfirmation ? 1 : 0));
         out.writeByte((byte) (sdesIsOn ? 1 : 0));
     }
@@ -168,13 +143,10 @@
         // Not used
     }
 
-    public void setZrtpNotSupported(boolean zrtpNotSupported) {
-        needSASConfirmation = false;
-        this.zrtpNotSupported = zrtpNotSupported;
-    }
-
-    public boolean isInitialized() {
-        return isInitialized;
+    public void setZrtpSupport(boolean support) {
+        zrtpIsSupported = support;
+        if(!support)
+            needSASConfirmation = false;
     }
 
     public void setInitialized() {
@@ -185,10 +157,11 @@
     * returns what state should be visible during call
     */
     public int displayModule() {
-        if (isInitialized()) {
+        if (isInitialized) {
+            Log.i("SecureSIp", "needSASConfirmation"+needSASConfirmation);
             if (needSASConfirmation) {
                 return DISPLAY_CONFIRM_SAS;
-            } else if (!zrtpNotSupported || sdesIsOn) {
+            } else if (zrtpIsSupported || sdesIsOn) {
                 return DISPLAY_GREEN_LOCK;
             } else {
                 return DISPLAY_RED_LOCK;
diff --git a/src/org/sflphone/service/CallManagerCallBack.java b/src/org/sflphone/service/CallManagerCallBack.java
index 6012aae..cc14c2f 100644
--- a/src/org/sflphone/service/CallManagerCallBack.java
+++ b/src/org/sflphone/service/CallManagerCallBack.java
@@ -284,6 +284,7 @@
         Intent intent = new Intent(ZRTP_ON);
         SecureSipCall call = (SecureSipCall) mService.getCallById(callID);
         call.setInitialized();
+        call.setZrtpSupport(true);
         intent.putExtra("callID", callID);
         intent.putExtra("conference", mService.findConference(callID));
         mService.sendBroadcast(intent);
@@ -294,6 +295,9 @@
         Log.i(TAG, "on_secure_zrtp_off");
         Intent intent = new Intent(ZRTP_OFF);
         intent.putExtra("callID", callID);
+        SecureSipCall call = (SecureSipCall) mService.getCallById(callID);
+        call.setInitialized();
+        call.setZrtpSupport(false);
         intent.putExtra("conference", mService.findConference(callID));
         mService.sendBroadcast(intent);
     }
@@ -319,7 +323,7 @@
         Intent intent = new Intent(ZRTP_NOT_SUPPORTED);
         SecureSipCall call = (SecureSipCall) mService.getCallById(callID);
         call.setInitialized();
-        call.setZrtpNotSupported(true);
+        call.setZrtpSupport(false);
         intent.putExtra("callID", callID);
         intent.putExtra("conference", mService.findConference(callID));
         mService.sendBroadcast(intent);
@@ -331,7 +335,7 @@
         Intent intent = new Intent(ZRTP_NEGOTIATION_FAILED);
         SecureSipCall call = (SecureSipCall) mService.getCallById(callID);
         call.setInitialized();
-        call.setZrtpNotSupported(true);
+        call.setZrtpSupport(false);
         intent.putExtra("callID", callID);
         intent.putExtra("conference", mService.findConference(callID));
         mService.sendBroadcast(intent);