X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/4460e43145ab8d50a46e3195a90002d292b58bfb..ac07e35d8ab68bf94d5cd8b45680ea69247fcc9f:/src/com/owncloud/android/files/services/FileDownloader.java diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index a07b0823..ed83d863 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -19,14 +19,15 @@ package com.owncloud.android.files.services; import java.io.File; +import java.io.IOException; import java.util.AbstractList; import java.util.Iterator; import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.webdav.OnDatatransferProgressListener; import com.owncloud.android.network.OwnCloudClientUtils; @@ -36,11 +37,11 @@ import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.fragment.FileDetailFragment; import android.accounts.Account; +import android.accounts.AccountsException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; -import android.content.ContentValues; import android.content.Intent; import android.os.Binder; import android.os.Handler; @@ -60,6 +61,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis public static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_FILE = "FILE"; + public static final String DOWNLOAD_ADDED_MESSAGE = "DOWNLOAD_ADDED"; public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH"; public static final String EXTRA_DOWNLOAD_RESULT = "RESULT"; public static final String EXTRA_FILE_PATH = "FILE_PATH"; @@ -73,6 +75,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis private IBinder mBinder; private WebdavClient mDownloadClient = null; private Account mLastAccount = null; + private FileDataStorageManager mStorageManager; private ConcurrentMap mPendingDownloads = new ConcurrentHashMap(); private DownloadFileOperation mCurrentDownload = null; @@ -135,6 +138,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mPendingDownloads.putIfAbsent(downloadKey, newDownload); newDownload.addDatatransferProgressListener(this); requestedDownloads.add(downloadKey); + sendBroadcastNewDownload(newDownload); } catch (IllegalArgumentException e) { Log.e(TAG, "Not enough information provided in intent: " + e.getMessage()); @@ -188,14 +192,28 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis /** - * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download + * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download. + * + * If 'file' is a directory, returns 'true' if some of its descendant files is downloading or waiting to download. * * @param account Owncloud account where the remote file is stored. * @param file A file that could be in the queue of downloads. */ public boolean isDownloading(Account account, OCFile file) { + if (account == null || file == null) return false; + String targetKey = buildRemoteName(account, file); synchronized (mPendingDownloads) { - return (mPendingDownloads.containsKey(buildRemoteName(account, file))); + if (file.isDirectory()) { + // this can be slow if there are many downloads :( + Iterator it = mPendingDownloads.keySet().iterator(); + boolean found = false; + while (it.hasNext() && !found) { + found = it.next().startsWith(targetKey); + } + return found; + } else { + return (mPendingDownloads.containsKey(targetKey)); + } } } } @@ -247,29 +265,30 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis notifyDownloadStart(mCurrentDownload); - /// prepare client object to send the request to the ownCloud server - if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) { - mLastAccount = mCurrentDownload.getAccount(); - mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext()); - } - - /// perform the download RemoteOperationResult downloadResult = null; try { - downloadResult = mCurrentDownload.execute(mDownloadClient); + /// prepare client object to send the request to the ownCloud server + if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) { + mLastAccount = mCurrentDownload.getAccount(); + mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver()); + mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext()); + } + + /// perform the download + if (downloadResult == null) { + downloadResult = mCurrentDownload.execute(mDownloadClient); + } if (downloadResult.isSuccess()) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, mCurrentDownload.getSavePath()); - getContentResolver().update( - ProviderTableMeta.CONTENT_URI, - cv, - ProviderTableMeta.FILE_NAME + "=? AND " - + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { - mCurrentDownload.getSavePath().substring(mCurrentDownload.getSavePath().lastIndexOf('/') + 1), - mLastAccount.name }); + saveDownloadedFile(); } + } catch (AccountsException e) { + Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e); + downloadResult = new RemoteOperationResult(e); + } catch (IOException e) { + Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e); + downloadResult = new RemoteOperationResult(e); + } finally { synchronized(mPendingDownloads) { mPendingDownloads.remove(downloadKey); @@ -280,11 +299,29 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis /// notify result notifyDownloadResult(mCurrentDownload, downloadResult); - sendFinalBroadcast(mCurrentDownload, downloadResult); + sendBroadcastDownloadFinished(mCurrentDownload, downloadResult); } } - + + /** + * Updates the OC File after a successful download. + */ + private void saveDownloadedFile() { + OCFile file = mCurrentDownload.getFile(); + long syncDate = System.currentTimeMillis(); + file.setLastSyncDateForProperties(syncDate); + file.setLastSyncDateForData(syncDate); + file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp()); + file.setModificationTimestampAtLastSyncForData(mCurrentDownload.getModificationTimestamp()); + // file.setEtag(mCurrentDownload.getEtag()); // TODO Etag, where available + file.setMimetype(mCurrentDownload.getMimeType()); + file.setStoragePath(mCurrentDownload.getSavePath()); + file.setFileLength((new File(mCurrentDownload.getSavePath()).length())); + mStorageManager.saveFile(file); + } + + /** * Creates a status notification to show the download progress * @@ -358,18 +395,32 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis /** - * Sends a broadcast in order to the interested activities can update their view + * Sends a broadcast when a download finishes in order to the interested activities can update their view * * @param download Finished download operation * @param downloadResult Result of the download operation */ - private void sendFinalBroadcast(DownloadFileOperation download, RemoteOperationResult downloadResult) { + private void sendBroadcastDownloadFinished(DownloadFileOperation download, RemoteOperationResult downloadResult) { Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); end.putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess()); end.putExtra(ACCOUNT_NAME, download.getAccount().name); end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath()); end.putExtra(EXTRA_FILE_PATH, download.getSavePath()); - sendBroadcast(end); + sendStickyBroadcast(end); + } + + + /** + * Sends a broadcast when a new download is added to the queue. + * + * @param download Added download operation + */ + private void sendBroadcastNewDownload(DownloadFileOperation download) { + Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE); + /*added.putExtra(ACCOUNT_NAME, download.getAccount().name); + added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/ + added.putExtra(EXTRA_FILE_PATH, download.getSavePath()); + sendStickyBroadcast(added); } }