X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/b0ab3ce0872d2702e82b5979dddaa5a897be340b..ce7f7fa48c5cce2070a8e42ec19d1954a18db090:/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 7343644d..d8fa84bb 100644
--- a/src/com/owncloud/android/files/services/FileDownloader.java
+++ b/src/com/owncloud/android/files/services/FileDownloader.java
@@ -1,14 +1,34 @@
+/* ownCloud Android client application
+ * Copyright (C) 2012 Bartek Przybylski
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+
package com.owncloud.android.files.services;
import java.io.File;
import java.util.AbstractList;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
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;
@@ -16,17 +36,16 @@ import com.owncloud.android.operations.DownloadFileOperation;
import com.owncloud.android.operations.RemoteOperationResult;
import com.owncloud.android.ui.activity.FileDetailActivity;
import com.owncloud.android.ui.fragment.FileDetailFragment;
+import com.owncloud.android.ui.preview.PreviewImageActivity;
+import com.owncloud.android.ui.preview.PreviewImageFragment;
import android.accounts.Account;
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.net.Uri;
import android.os.Binder;
-import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
@@ -36,6 +55,7 @@ import android.os.Process;
import android.util.Log;
import android.widget.RemoteViews;
+import com.owncloud.android.AccountUtils;
import com.owncloud.android.R;
import eu.alefzero.webdav.WebdavClient;
@@ -44,6 +64,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";
@@ -57,6 +78,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;
@@ -75,18 +97,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
private String buildRemoteName(Account account, OCFile file) {
return account.name + file.getRemotePath();
}
-
- public static final String getSavePath(String accountName) {
- File sdCard = Environment.getExternalStorageDirectory();
- return sdCard.getAbsolutePath() + "/owncloud/" + Uri.encode(accountName, "@");
- // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
- }
-
- public static final String getTemporalPath(String accountName) {
- File sdCard = Environment.getExternalStorageDirectory();
- return sdCard.getAbsolutePath() + "/owncloud/tmp/" + Uri.encode(accountName, "@");
- // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
- }
/**
@@ -130,7 +140,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
DownloadFileOperation newDownload = new DownloadFileOperation(account, file);
mPendingDownloads.putIfAbsent(downloadKey, newDownload);
newDownload.addDatatransferProgressListener(this);
+ newDownload.addDatatransferProgressListener((FileDownloaderBinder)mBinder);
requestedDownloads.add(downloadKey);
+ sendBroadcastNewDownload(newDownload);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Not enough information provided in intent: " + e.getMessage());
@@ -158,13 +170,29 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
return mBinder;
}
+
+ /**
+ * Called when ALL the bound clients were onbound.
+ */
+ @Override
+ public boolean onUnbind(Intent intent) {
+ ((FileDownloaderBinder)mBinder).clearListeners();
+ return false; // not accepting rebinding (default behaviour)
+ }
+
/**
* Binder to let client components to perform operations on the queue of downloads.
*
* It provides by itself the available operations.
*/
- public class FileDownloaderBinder extends Binder {
+ public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {
+
+ /**
+ * Map of listeners that will be reported about progress of downloads from a {@link FileDownloaderBinder} instance
+ */
+ private Map mBoundListeners = new HashMap();
+
/**
* Cancels a pending or current download of a remote file.
@@ -183,17 +211,85 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
}
+ public void clearListeners() {
+ mBoundListeners.clear();
+ }
+
+
/**
- * 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));
+ }
+ }
+ }
+
+
+ /**
+ * Adds a listener interested in the progress of the download for a concrete file.
+ *
+ * @param listener Object to notify about progress of transfer.
+ * @param account ownCloud account holding the file of interest.
+ * @param file {@link OCfile} of interest for listener.
+ */
+ public void addDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
+ if (account == null || file == null || listener == null) return;
+ String targetKey = buildRemoteName(account, file);
+ mBoundListeners.put(targetKey, listener);
+ }
+
+
+
+ /**
+ * Removes a listener interested in the progress of the download for a concrete file.
+ *
+ * @param listener Object to notify about progress of transfer.
+ * @param account ownCloud account holding the file of interest.
+ * @param file {@link OCfile} of interest for listener.
+ */
+ public void removeDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
+ if (account == null || file == null || listener == null) return;
+ String targetKey = buildRemoteName(account, file);
+ if (mBoundListeners.get(targetKey) == listener) {
+ mBoundListeners.remove(targetKey);
}
}
+
+
+ @Override
+ public void onTransferProgress(long progressRate) {
+ // old way, should not be in use any more
+ }
+
+
+ @Override
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,
+ String fileName) {
+ String key = buildRemoteName(mCurrentDownload.getAccount(), mCurrentDownload.getFile());
+ OnDatatransferProgressListener boundListener = mBoundListeners.get(key);
+ if (boundListener != null) {
+ boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);
+ }
+ }
+
}
@@ -246,6 +342,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
/// 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());
}
@@ -254,16 +351,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
try {
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();
}
} finally {
@@ -276,11 +364,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
*
@@ -297,11 +403,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
/// includes a pending intent in the notification showing the details view of the file
- Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
+ Intent showDetailsIntent = null;
+ if (PreviewImageFragment.canBePreviewed(download.getFile())) {
+ showDetailsIntent = new Intent(this, PreviewImageActivity.class);
+ } else {
+ showDetailsIntent = new Intent(this, FileDetailActivity.class);
+ }
showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());
showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());
showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, showDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);
mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification);
}
@@ -345,8 +456,22 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;
Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis());
finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;
- // TODO put something smart in the contentIntent below
- finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
+ Intent showDetailsIntent = null;
+ if (downloadResult.isSuccess()) {
+ if (PreviewImageFragment.canBePreviewed(download.getFile())) {
+ showDetailsIntent = new Intent(this, PreviewImageActivity.class);
+ } else {
+ showDetailsIntent = new Intent(this, FileDetailActivity.class);
+ }
+ showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());
+ showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());
+ showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+
+ } else {
+ // TODO put something smart in showDetailsIntent
+ showDetailsIntent = new Intent();
+ }
+ finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);
finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent);
mNotificationManager.notify(tickerId, finalNotification);
}
@@ -354,20 +479,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());
- if (downloadResult.isSuccess()) {
- end.putExtra(EXTRA_FILE_PATH, download.getSavePath());
- }
- sendBroadcast(end);
+ end.putExtra(EXTRA_FILE_PATH, download.getSavePath());
+ 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);
}
}