delete file after download failed, correctly send message about download fail

This commit is contained in:
Bartek Przybylski 2012-07-10 18:07:05 +02:00
parent 233553a081
commit a9a1ad71e9
4 changed files with 30 additions and 35 deletions

View file

@ -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);
}

View file

@ -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);

View file

@ -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();
}
}

View file

@ -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;
}
BufferedInputStream bis = new BufferedInputStream(
get.getResponseBodyAsStream());
FileOutputStream fos = new FileOutputStream(targetPath);
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);
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;
}
/**