\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
\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
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
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
@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
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
}\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
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