am 36bcc2e2: am 4124f4a4: Merge "Specify a timeout for the connection handshake" into jb-mr1.1-dev

* commit '36bcc2e29ab36ae3626443bd12ad173b0ed08808':
  Specify a timeout for the connection handshake
diff --git a/emailcommon/src/com/android/emailcommon/utility/EmailClientConnectionManager.java b/emailcommon/src/com/android/emailcommon/utility/EmailClientConnectionManager.java
index 15d1cca..55ae014 100644
--- a/emailcommon/src/com/android/emailcommon/utility/EmailClientConnectionManager.java
+++ b/emailcommon/src/com/android/emailcommon/utility/EmailClientConnectionManager.java
@@ -99,7 +99,7 @@
             KeyManager keyManager =
                     KeyChainKeyManager.fromAlias(context, hostAuth.mClientCertAlias);
             SSLCertificateSocketFactory underlying = SSLUtils.getSSLSocketFactory(
-                    hostAuth.shouldTrustAllServerCerts());
+                    hostAuth.shouldTrustAllServerCerts(), 0 /* no timeout */);
             underlying.setKeyManagers(new KeyManager[] { keyManager });
             registry.register(
                     new Scheme(schemeName, new SSLSocketFactory(underlying), hostAuth.mPort));
diff --git a/emailcommon/src/com/android/emailcommon/utility/SSLUtils.java b/emailcommon/src/com/android/emailcommon/utility/SSLUtils.java
index b21c68f..85b77c1 100644
--- a/emailcommon/src/com/android/emailcommon/utility/SSLUtils.java
+++ b/emailcommon/src/com/android/emailcommon/utility/SSLUtils.java
@@ -47,19 +47,20 @@
      * Optionally bypass all SSL certificate checks.
      *
      * @param insecure if true, bypass all SSL certificate checks
+     * @param timeout the timeout value in milliseconds or {@code 0} for an infinite timeout.
      */
     public synchronized static SSLCertificateSocketFactory getSSLSocketFactory(
-            boolean insecure) {
+            boolean insecure, int timeout) {
         if (insecure) {
             if (sInsecureFactory == null) {
                 sInsecureFactory = (SSLCertificateSocketFactory)
-                        SSLCertificateSocketFactory.getInsecure(0, null);
+                        SSLCertificateSocketFactory.getInsecure(timeout, null);
             }
             return sInsecureFactory;
         } else {
             if (sSecureFactory == null) {
                 sSecureFactory = (SSLCertificateSocketFactory)
-                        SSLCertificateSocketFactory.getDefault(0, null);
+                        SSLCertificateSocketFactory.getDefault(timeout, null);
             }
             return sSecureFactory;
         }
@@ -70,7 +71,7 @@
      * Apache HTTP stack.
      */
     public static SSLSocketFactory getHttpSocketFactory(boolean insecure, KeyManager keyManager) {
-        SSLCertificateSocketFactory underlying = getSSLSocketFactory(insecure);
+        SSLCertificateSocketFactory underlying = getSSLSocketFactory(insecure, 0 /* no timeout */);
         if (keyManager != null) {
             underlying.setKeyManagers(new KeyManager[] { keyManager });
         }
diff --git a/src/com/android/email/mail/transport/MailTransport.java b/src/com/android/email/mail/transport/MailTransport.java
index 751be50..3f4d7bc 100644
--- a/src/com/android/email/mail/transport/MailTransport.java
+++ b/src/com/android/email/mail/transport/MailTransport.java
@@ -167,7 +167,8 @@
         try {
             SocketAddress socketAddress = new InetSocketAddress(getHost(), getPort());
             if (canTrySslSecurity()) {
-                mSocket = SSLUtils.getSSLSocketFactory(canTrustAllCertificates()).createSocket();
+                mSocket = SSLUtils.getSSLSocketFactory(
+                        canTrustAllCertificates(), SOCKET_CONNECT_TIMEOUT).createSocket();
             } else {
                 mSocket = new Socket();
             }
@@ -203,8 +204,9 @@
     @Override
     public void reopenTls() throws MessagingException {
         try {
-            mSocket = SSLUtils.getSSLSocketFactory(canTrustAllCertificates())
-                    .createSocket(mSocket, getHost(), getPort(), true);
+            mSocket =
+                    SSLUtils.getSSLSocketFactory(canTrustAllCertificates(), SOCKET_CONNECT_TIMEOUT)
+                            .createSocket(mSocket, getHost(), getPort(), true);
             mSocket.setSoTimeout(SOCKET_READ_TIMEOUT);
             mIn = new BufferedInputStream(mSocket.getInputStream(), 1024);
             mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512);
@@ -281,20 +283,34 @@
      */
     @Override
     public void close() {
+        if (Email.DEBUG) {
+            Log.d(Logging.LOG_TAG, "*** " + mDebugLabel + " close " +
+                    getHost() + ":" + String.valueOf(getPort()));
+        }
+
         try {
             mIn.close();
         } catch (Exception e) {
             // May fail if the connection is already closed.
+            if (Email.DEBUG) {
+                Log.d(Logging.LOG_TAG, e.toString());
+            }
         }
         try {
             mOut.close();
         } catch (Exception e) {
             // May fail if the connection is already closed.
+            if (Email.DEBUG) {
+                Log.d(Logging.LOG_TAG, e.toString());
+            }
         }
         try {
             mSocket.close();
         } catch (Exception e) {
             // May fail if the connection is already closed.
+            if (Email.DEBUG) {
+                Log.d(Logging.LOG_TAG, e.toString());
+            }
         }
         mIn = null;
         mOut = null;