package com.android.email.mail.transport;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.params.HttpParams;

import com.android.email.mail.store.TrustManagerFactory;

public class TrustedSocketFactory implements SocketFactory {
	private SSLSocketFactory mSocketFactory;
	private org.apache.http.conn.ssl.SSLSocketFactory mSchemeSocketFactory;
	
	public TrustedSocketFactory(String host, boolean secure) throws NoSuchAlgorithmException, KeyManagementException{
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] {
                TrustManagerFactory.get(host, secure)
        }, new SecureRandom());
        mSocketFactory = sslContext.getSocketFactory();
        mSchemeSocketFactory = org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
        mSchemeSocketFactory.setHostnameVerifier(
        		org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
	}

	@Override
	public Socket connectSocket(Socket sock, String host, int port,
			InetAddress localAddress, int localPort, HttpParams params)
			throws IOException, UnknownHostException, ConnectTimeoutException {
		return mSchemeSocketFactory.connectSocket(sock, host, port, localAddress, localPort, params);
	}

	@Override
	public Socket createSocket() throws IOException {
		// TODO Auto-generated method stub
		return mSocketFactory.createSocket();
	}

	@Override
	public boolean isSecure(Socket arg0) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return false;
	}
	
}

@@ -126,40 +126,8 @@
 t;%0D%0A
-import java.net.SocketAddress;%0D%0A
 impo
@@ -1404,36 +1404,24 @@
 ER);%0D%0A%09%7D%0D%0A%0D%0A
-%09@Override%0D%0A
 %09public Sock
@@ -1709,36 +1709,24 @@
 ms);%0D%0A%09%7D%0D%0A%0D%0A
-%09@Override%0D%0A
 %09public Sock
@@ -1769,46 +1769,8 @@
  %7B%0D%0A
-%09%09// TODO Auto-generated method stub%0D%0A
 %09%09re
@@ -1816,20 +1816,8 @@
 %0D%0A%0D%0A
-%09@Override%0D%0A
 %09pub
@@ -1848,12 +1848,12 @@
 ket 
-arg0
+sock
 ) th
@@ -1891,58 +1891,50 @@
 %0D%0A%09%09
-// TODO Auto-generated method stub%0D%0A%09%09return false
+return mSchemeSocketFactory.isSecure(sock)
 ;%0D%0A%09

remove @Override : this causes breakage in jdk 1.5.