delete file after download failed, correctly send message about download fail
authorBartek Przybylski <bart.p.pl@gmail.com>
Tue, 10 Jul 2012 16:07:05 +0000 (18:07 +0200)
committerBartek Przybylski <bart.p.pl@gmail.com>
Tue, 10 Jul 2012 16:07:05 +0000 (18:07 +0200)
src/eu/alefzero/owncloud/files/services/FileDownloader.java
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java
src/eu/alefzero/webdav/WebdavClient.java

index 7a88f6a..9c9b3ef 100644 (file)
@@ -28,7 +28,7 @@ import eu.alefzero.webdav.WebdavClient;
 \r
 public class FileDownloader extends Service implements OnDatatransferProgressListener {\r
     public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";\r
-    public static final String BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD";    \r
+    public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";    \r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
     public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";\r
@@ -127,14 +127,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 \r
         File sdCard = Environment.getExternalStorageDirectory();\r
         File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);\r
-        try {\r
-            file.getParentFile().mkdirs();\r
-            file.createNewFile();\r
-        } catch (IOException e) {\r
-            e.printStackTrace();\r
-        }\r
+        file.getParentFile().mkdirs();\r
 \r
-        String message;\r
+        boolean download_result = false;\r
         if (wdc.downloadFile(mRemotePath, file)) {\r
             ContentValues cv = new ContentValues();\r
             cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
@@ -146,15 +141,13 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                     new String[] {\r
                             mFilePath.substring(mFilePath.lastIndexOf('/') + 1),\r
                             mAccount.name });            \r
-            message = DOWNLOAD_FINISH_MESSAGE;\r
-        } else {\r
-            file.delete();\r
-            message = BAD_DOWNLOAD_MESSAGE;\r
+            download_result = true;\r
         }\r
         \r
         mNotificationMngr.cancel(1);\r
-        Intent end = new Intent(message);\r
+        Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
         end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());\r
+        end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);\r
         sendBroadcast(end);\r
     }\r
 \r
index a5b1fc8..c94c27f 100644 (file)
@@ -113,7 +113,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
         setSupportProgressBarIndeterminateVisibility(false);\r
 \r
-//        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));\r
+        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));\r
 \r
         if(savedInstanceState != null) {\r
             mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY);\r
index 8dcdb9c..cd26b2a 100644 (file)
@@ -186,7 +186,7 @@ public class FileDetailFragment extends SherlockFragment implements
     @Override\r
     public void onClick(View v) {\r
         if (v.getId() == R.id.fdDownloadBtn) {\r
-            Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();\r
+            //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();\r
             Intent i = new Intent(getActivity(), FileDownloader.class);\r
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
             i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
@@ -466,12 +466,11 @@ public class FileDetailFragment extends SherlockFragment implements
             if (getView()!=null && getView().findViewById(R.id.fdDownloadBtn) != null) \r
                 getView().findViewById(R.id.fdDownloadBtn).setEnabled(true);\r
             \r
-            if (intent.getAction().equals(FileDownloader.BAD_DOWNLOAD_MESSAGE)) {\r
-                Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
-                \r
-            } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+            if (intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)) {\r
                 mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
                 updateFileDetails();\r
+            } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+                Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
             }\r
         }\r
         \r
index 8d2934c..dd3b4ea 100644 (file)
@@ -114,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
@@ -125,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