X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/93983377ccbedb14102d34144e83d4d28c5c2cab..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 7a88f6ab..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; @@ -20,19 +23,22 @@ import android.os.Message; import android.os.Process; import android.util.Log; import android.widget.RemoteViews; +import android.widget.Toast; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; +import eu.alefzero.owncloud.syncadapter.FileSyncService; 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"; public static final String EXTRA_FILE_SIZE = "FILE_SIZE"; + public static final String ACCOUNT_NAME = "ACCOUNT_NAME"; private static final String TAG = "FileDownloader"; private NotificationManager mNotificationMngr; @@ -45,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); @@ -57,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() { @@ -125,37 +161,51 @@ 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); - try { - file.getParentFile().mkdirs(); - file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - - String message; - if (wdc.downloadFile(mRemotePath, file)) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); - getContentResolver().update( + // 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; + 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 }); - message = DOWNLOAD_FINISH_MESSAGE; - } else { - file.delete(); - message = BAD_DOWNLOAD_MESSAGE; + mAccount.name }); + download_result = true; + } } + mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath)); + mNotificationMngr.cancel(1); - Intent end = new Intent(message); - end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); + Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); + 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) { + Toast.makeText(this, R.string.downloader_download_succeed , Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(this, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show(); + } + } @Override @@ -170,5 +220,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mLastPercent = percent; } - + + }