From: Bartek Przybylski Date: Tue, 10 Jul 2012 16:07:05 +0000 (+0200) Subject: delete file after download failed, correctly send message about download fail X-Git-Tag: oc-android-1.4.3~293 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/a9a1ad71e9495be4ed6d3e4033456d1161904c7c?ds=inline;hp=-c delete file after download failed, correctly send message about download fail --- a9a1ad71e9495be4ed6d3e4033456d1161904c7c diff --git a/src/eu/alefzero/owncloud/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java index 7a88f6ab..9c9b3eff 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -28,7 +28,7 @@ import eu.alefzero.webdav.WebdavClient; public class FileDownloader extends Service implements OnDatatransferProgressListener { public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH"; - public static final String BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD"; + public static final String EXTRA_DOWNLOAD_RESULT = "RESULT"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_FILE_PATH = "FILE_PATH"; public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH"; @@ -127,14 +127,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis File sdCard = Environment.getExternalStorageDirectory(); File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath); - try { - file.getParentFile().mkdirs(); - file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } + file.getParentFile().mkdirs(); - String message; + boolean download_result = false; if (wdc.downloadFile(mRemotePath, file)) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); @@ -146,15 +141,13 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis new String[] { mFilePath.substring(mFilePath.lastIndexOf('/') + 1), mAccount.name }); - message = DOWNLOAD_FINISH_MESSAGE; - } else { - file.delete(); - message = BAD_DOWNLOAD_MESSAGE; + download_result = true; } mNotificationMngr.cancel(1); - Intent end = new Intent(message); + Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); + end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result); sendBroadcast(end); } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index a5b1fc8d..c94c27fa 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -113,7 +113,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setSupportProgressBarIndeterminateVisibility(false); -// Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext())); + Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext())); if(savedInstanceState != null) { mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY); diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 8dcdb9cc..cd26b2a1 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -186,7 +186,7 @@ public class FileDetailFragment extends SherlockFragment implements @Override public void onClick(View v) { if (v.getId() == R.id.fdDownloadBtn) { - Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show(); + //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show(); Intent i = new Intent(getActivity(), FileDownloader.class); i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath()); @@ -466,12 +466,11 @@ public class FileDetailFragment extends SherlockFragment implements if (getView()!=null && getView().findViewById(R.id.fdDownloadBtn) != null) getView().findViewById(R.id.fdDownloadBtn).setEnabled(true); - if (intent.getAction().equals(FileDownloader.BAD_DOWNLOAD_MESSAGE)) { - Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show(); - - } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) { + if (intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)) { mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH)); updateFileDetails(); + } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) { + Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show(); } } diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 8d2934c1..dd3b4eae 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -114,6 +114,7 @@ public class WebdavClient extends HttpClient { } public boolean downloadFile(String remoteFilepath, File targetPath) { + boolean ret = false; GetMethod get = new GetMethod(mUri.toString() + remoteFilepath); HttpMethodParams params = get.getParams(); params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit @@ -125,26 +126,28 @@ public class WebdavClient extends HttpClient { try { int status = executeMethod(get); Log.e(TAG, "status return: " + status); - if (status != HttpStatus.SC_OK) { - return false; + if (status == HttpStatus.SC_OK) { + targetPath.createNewFile(); + BufferedInputStream bis = new BufferedInputStream( + get.getResponseBodyAsStream()); + FileOutputStream fos = new FileOutputStream(targetPath); + + byte[] bytes = new byte[4096]; + int readResult; + while ((readResult = bis.read(bytes)) != -1) { + if (mDataTransferListener != null) + mDataTransferListener.transferProgress(readResult); + fos.write(bytes, 0, readResult); + } + } - BufferedInputStream bis = new BufferedInputStream( - get.getResponseBodyAsStream()); - FileOutputStream fos = new FileOutputStream(targetPath); - - byte[] bytes = new byte[4096]; - int readResult; - while ((readResult = bis.read(bytes)) != -1) { - if (mDataTransferListener != null) - mDataTransferListener.transferProgress(readResult); - fos.write(bytes, 0, readResult); - } - - } catch (IOException e) { + ret = true; + } catch (Throwable e) { e.printStackTrace(); - return false; + targetPath.delete(); } - return true; + + return ret; } /**