X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/7382fa84b046ebdd7fd79334ace16553b0bffc09..aef73a5263c94e34a38bc45ce1506651d80c7f78:/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 50a8c49f..a07b0823 100644
--- a/src/com/owncloud/android/files/services/FileDownloader.java
+++ b/src/com/owncloud/android/files/services/FileDownloader.java
@@ -1,3 +1,21 @@
+/* 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;
@@ -24,9 +42,7 @@ 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;
@@ -40,6 +56,7 @@ import com.owncloud.android.R;
import eu.alefzero.webdav.WebdavClient;
public class FileDownloader extends Service implements OnDatatransferProgressListener {
+
public static final String EXTRA_ACCOUNT = "ACCOUNT";
public static final String EXTRA_FILE = "FILE";
@@ -60,29 +77,20 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
private ConcurrentMap mPendingDownloads = new ConcurrentHashMap();
private DownloadFileOperation mCurrentDownload = null;
- private NotificationManager mNotificationMngr;
+ private NotificationManager mNotificationManager;
private Notification mNotification;
private int mLastPercent;
/**
- * Builds a key for mDownloadsInProgress from the accountName and remotePath
+ * Builds a key for mPendingDownloads from the account and file to download
+ *
+ * @param account Account where the file to download is stored
+ * @param file File to download
*/
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
- }
/**
@@ -91,12 +99,12 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
@Override
public void onCreate() {
super.onCreate();
- mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- HandlerThread thread = new HandlerThread("FileDownladerThread",
+ mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+ HandlerThread thread = new HandlerThread("FileDownloaderThread",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
+ mServiceHandler = new ServiceHandler(mServiceLooper, this);
mBinder = new FileDownloaderBinder();
}
@@ -180,10 +188,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
/**
- * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading
+ * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download
*
* @param account Owncloud account where the remote file is stored.
- * @param file A file in the queue of downloads.
+ * @param file A file that could be in the queue of downloads.
*/
public boolean isDownloading(Account account, OCFile file) {
synchronized (mPendingDownloads) {
@@ -198,9 +206,14 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
*
* Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
*/
- private final class ServiceHandler extends Handler {
- public ServiceHandler(Looper looper) {
+ private static class ServiceHandler extends Handler {
+ // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
+ FileDownloader mService;
+ public ServiceHandler(Looper looper, FileDownloader service) {
super(looper);
+ if (service == null)
+ throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
+ mService = service;
}
@Override
@@ -210,10 +223,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
if (msg.obj != null) {
Iterator it = requestedDownloads.iterator();
while (it.hasNext()) {
- downloadFile(it.next());
+ mService.downloadFile(it.next());
}
}
- stopSelf(msg.arg1);
+ mService.stopSelf(msg.arg1);
}
}
@@ -235,7 +248,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
notifyDownloadStart(mCurrentDownload);
/// prepare client object to send the request to the ownCloud server
- if (mDownloadClient == null || mLastAccount != mCurrentDownload.getAccount()) {
+ if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {
mLastAccount = mCurrentDownload.getAccount();
mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
}
@@ -258,7 +271,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
}
} finally {
- mPendingDownloads.remove(downloadKey);
+ synchronized(mPendingDownloads) {
+ mPendingDownloads.remove(downloadKey);
+ }
}
@@ -271,41 +286,17 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
/**
- * Callback method to update the progress bar in the status notification.
- */
- @Override
- public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
- int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
- if (percent != mLastPercent) {
- mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, totalToTransfer == -1);
- mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName));
- mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification);
- }
- mLastPercent = percent;
- }
-
-
- /**
- * Callback method to update the progress bar in the status notification (old version)
- */
- @Override
- public void onTransferProgress(long progressRate) {
- // NOTHING TO DO HERE ANYMORE
- }
-
-
- /**
* Creates a status notification to show the download progress
*
* @param download Download operation starting.
*/
private void notifyDownloadStart(DownloadFileOperation download) {
- /// create status notification to show the download progress
+ /// create status notification with a progress bar
mLastPercent = 0;
mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
- mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, download.getSize() == -1);
+ mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, download.getSize() < 0);
mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), 0, new File(download.getSavePath()).getName()));
mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
@@ -314,29 +305,54 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
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);
- mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification);
+ mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification);
}
/**
+ * Callback method to update the progress bar in the status notification.
+ */
+ @Override
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
+ int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
+ if (percent != mLastPercent) {
+ mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, totalToTransfer < 0);
+ String text = String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName);
+ mNotification.contentView.setTextViewText(R.id.status_text, text);
+ mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification);
+ }
+ mLastPercent = percent;
+ }
+
+
+ /**
+ * Callback method to update the progress bar in the status notification (old version)
+ */
+ @Override
+ public void onTransferProgress(long progressRate) {
+ // NOTHING TO DO HERE ANYMORE
+ }
+
+
+ /**
* Updates the status notification with the result of a download operation.
*
* @param downloadResult Result of the download operation.
* @param download Finished download operation
*/
private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
- mNotificationMngr.cancel(R.string.downloader_download_in_progress_ticker);
+ mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
if (!downloadResult.isCancelled()) {
int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
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);
+ finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent);
- mNotificationMngr.notify(tickerId, finalNotification);
+ mNotificationManager.notify(tickerId, finalNotification);
}
}
@@ -352,9 +368,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
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());
- }
+ end.putExtra(EXTRA_FILE_PATH, download.getSavePath());
sendBroadcast(end);
}