X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/8ba2ca7b8556de4aea4a7a8407a204b4dcfca34c..a238d0635ad16d6adc97d08ef0fa89b8afb18b46:/src/eu/alefzero/owncloud/files/services/FileDownloader.java diff --git a/src/eu/alefzero/owncloud/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java index d08e7279..c147c9d2 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -2,6 +2,9 @@ package eu.alefzero.owncloud.files.services; import java.io.File; import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import android.accounts.Account; import android.accounts.AccountManager; @@ -48,7 +51,27 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis private long mTotalDownloadSize; private long mCurrentDownlodSize; private Notification mNotification; + + /** + * Static map with the files being download and the path to the temporal file were are download + */ + private static Map mDownloadsInProgress = Collections.synchronizedMap(new HashMap()); + + /** + * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading + */ + public static boolean isDownloading(Account account, String remotePath) { + return (mDownloadsInProgress.get(buildRemoteName(account.name, remotePath)) != null); + } + + /** + * Builds a key for mDownloadsInProgress from the accountName and remotePath + */ + private static String buildRemoteName(String accountName, String remotePath) { + return accountName + remotePath; + } + private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); @@ -60,6 +83,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis stopSelf(msg.arg1); } } + + public static final String getSavePath() { + File sdCard = Environment.getExternalStorageDirectory(); + return sdCard.getAbsolutePath() + "/owncloud/"; + } + + public static final String getTemporalPath() { + File sdCard = Environment.getExternalStorageDirectory(); + return sdCard.getAbsolutePath() + "/owncloud.tmp/"; + } @Override public void onCreate() { @@ -128,31 +161,43 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mNotificationMngr.notify(1, mNotification); - File sdCard = Environment.getExternalStorageDirectory(); - File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath); - file.getParentFile().mkdirs(); + // download in a temporal file + File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath); + tmpFile.getParentFile().mkdirs(); + mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath()); boolean download_result = false; - if (wdc.downloadFile(mRemotePath, file)) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); - getContentResolver().update( + File newFile = null; + if (wdc.downloadFile(mRemotePath, tmpFile)) { + newFile = new File(getSavePath() + mAccount.name + mFilePath); + newFile.getParentFile().mkdirs(); + boolean moved = tmpFile.renameTo(newFile); + + if (moved) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newFile.getAbsolutePath()); + getContentResolver().update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta.FILE_NAME + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] { mFilePath.substring(mFilePath.lastIndexOf('/') + 1), - mAccount.name }); - download_result = true; + mAccount.name }); + download_result = true; + } } + mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath)); + mNotificationMngr.cancel(1); Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); - end.putExtra(EXTRA_REMOTE_PATH, mRemotePath); - end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result); end.putExtra(ACCOUNT_NAME, mAccount.name); + end.putExtra(EXTRA_REMOTE_PATH, mRemotePath); + if (download_result) { + end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath()); + } sendBroadcast(end); if (download_result) {