Avoid the progress circle is visible when starting the app after updating it in the...
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
index fd1ec50..dd3b4ea 100644 (file)
@@ -21,10 +21,12 @@ import java.io.BufferedInputStream;
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
-import java.util.HashMap;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
 import org.apache.commons.httpclient.HttpClient;\r
+import org.apache.commons.httpclient.HttpConnectionManager;\r
+import org.apache.commons.httpclient.HttpVersion;\r
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;\r
 import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
 import org.apache.commons.httpclient.auth.AuthScope;\r
 import org.apache.commons.httpclient.methods.GetMethod;\r
@@ -33,6 +35,7 @@ import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.params.HttpMethodParams;\r
 import org.apache.commons.httpclient.protocol.Protocol;\r
 import org.apache.http.HttpStatus;\r
+import org.apache.http.params.CoreProtocolPNames;\r
 import org.apache.jackrabbit.webdav.client.methods.DavMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;\r
@@ -54,7 +57,16 @@ public class WebdavClient extends HttpClient {
     final private static String TAG = "WebdavClient";\r
     private static final String USER_AGENT = "Android-ownCloud";\r
     private OnDatatransferProgressListener mDataTransferListener;\r
-    private static HashMap<String, WebdavClient> clients = new HashMap<String, WebdavClient>();\r
+    static private MultiThreadedHttpConnectionManager mConnManager = null;\r
+    \r
+    static public MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {\r
+        if (mConnManager == null) {\r
+            mConnManager = new MultiThreadedHttpConnectionManager();\r
+            mConnManager.setMaxConnectionsPerHost(5);\r
+            mConnManager.setMaxTotalConnections(5);\r
+        }\r
+        return mConnManager;\r
+    }\r
     \r
     /**\r
      * Creates a WebdavClient setup for the current account\r
@@ -62,7 +74,7 @@ public class WebdavClient extends HttpClient {
      * @param context The application context\r
      * @return\r
      */\r
-    public WebdavClient (Account account, Context context){\r
+    public WebdavClient (Account account, Context context) {\r
         OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account,\r
                 AccountAuthenticator.KEY_OC_VERSION));\r
         String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
@@ -71,15 +83,20 @@ public class WebdavClient extends HttpClient {
         String password = AccountManager.get(context).getPassword(account);\r
         \r
         mUri = Uri.parse(baseUrl + webDavPath);\r
-        getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);\r
+\r
         setCredentials(username, password);\r
-        allowSelfsignedCertificates();\r
     }\r
     \r
-    public WebdavClient(){}\r
+    public WebdavClient() {\r
+        super(getMultiThreadedConnManager());\r
+        \r
+        getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);\r
+        getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);\r
+        allowSelfsignedCertificates();\r
+    }\r
 \r
     public void setCredentials(String username, String password) {\r
-        getParams().setAuthenticationPreemptive(true);\r
+        //getParams().setAuthenticationPreemptive(true);\r
         getState().setCredentials(AuthScope.ANY,\r
                 getCredentials(username, password));\r
     }\r
@@ -97,6 +114,7 @@ public class WebdavClient extends HttpClient {
     }\r
 \r
     public boolean downloadFile(String remoteFilepath, File targetPath) {\r
+        boolean ret = false;\r
         GetMethod get = new GetMethod(mUri.toString() + remoteFilepath);\r
         HttpMethodParams params = get.getParams();\r
         params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit\r
@@ -108,26 +126,28 @@ public class WebdavClient extends HttpClient {
         try {\r
             int status = executeMethod(get);\r
             Log.e(TAG, "status return: " + status);\r
-            if (status != HttpStatus.SC_OK) {\r
-                return false;\r
+            if (status == HttpStatus.SC_OK) {\r
+                targetPath.createNewFile();\r
+                BufferedInputStream bis = new BufferedInputStream(\r
+                        get.getResponseBodyAsStream());\r
+                FileOutputStream fos = new FileOutputStream(targetPath);\r
+\r
+                byte[] bytes = new byte[4096];\r
+                int readResult;\r
+                while ((readResult = bis.read(bytes)) != -1) {\r
+                    if (mDataTransferListener != null)\r
+                        mDataTransferListener.transferProgress(readResult);\r
+                    fos.write(bytes, 0, readResult);\r
+                }\r
+                \r
             }\r
-            BufferedInputStream bis = new BufferedInputStream(\r
-                    get.getResponseBodyAsStream());\r
-            FileOutputStream fos = new FileOutputStream(targetPath);\r
-\r
-            byte[] bytes = new byte[4096];\r
-            int readResult;\r
-            while ((readResult = bis.read(bytes)) != -1) {\r
-                if (mDataTransferListener != null)\r
-                    mDataTransferListener.transferProgress(readResult);\r
-                fos.write(bytes, 0, readResult);\r
-            }\r
-\r
-        } catch (IOException e) {\r
+            ret = true;\r
+        } catch (Throwable e) {\r
             e.printStackTrace();\r
-            return false;\r
+            targetPath.delete();\r
         }\r
-        return true;\r
+        \r
+        return ret;\r
     }\r
     \r
     /**\r