Added a static list of downloads in progress to have a reliable way to check if a file is downloading

This commit is contained in:
David A. Velasco 2012-07-24 14:08:54 +02:00
parent 9d3208ea03
commit a238d0635a
5 changed files with 34 additions and 17 deletions

View file

@ -18,7 +18,7 @@
-->
<manifest package="eu.alefzero.owncloud"
android:versionCode="1"
android:versionName="0.1.177B" xmlns:android="http://schemas.android.com/apk/res/android">
android:versionName="0.1.178B" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

View file

@ -156,20 +156,6 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
return false;
}
/**
* Use this to check if this file is downloading
*
* @return true if it is in a download in progress
*/
public boolean isDownloading() {
if (mLocalPath != null && mLocalPath.length() > 0) {
String savePath = FileDownloader.getSavePath();
File file = new File(FileDownloader.getTemporalPath() + mLocalPath.substring(savePath.length()));
return (file.exists());
}
return false;
}
/**
* The path, where the file is stored locally
*

View file

@ -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;
@ -49,6 +52,26 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
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<String, String> mDownloadsInProgress = Collections.synchronizedMap(new HashMap<String, String>());
/**
* 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);
@ -141,6 +164,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
// 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;
@ -164,6 +188,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
}
}
mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath));
mNotificationMngr.cancel(1);
Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);

View file

@ -19,11 +19,14 @@ package eu.alefzero.owncloud.ui.adapter;
import java.util.Vector;
import eu.alefzero.owncloud.AccountUtils;
import eu.alefzero.owncloud.DisplayUtils;
import eu.alefzero.owncloud.R;
import eu.alefzero.owncloud.datamodel.DataStorageManager;
import eu.alefzero.owncloud.datamodel.OCFile;
import eu.alefzero.owncloud.files.services.FileDownloader;
import android.accounts.Account;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
@ -46,6 +49,7 @@ public class FileListListAdapter implements ListAdapter {
private OCFile mFile;
private Vector<OCFile> mFiles;
private DataStorageManager mStorageManager;
private Account mAccount;
public FileListListAdapter(OCFile file, DataStorageManager storage_man,
Context context) {
@ -53,6 +57,7 @@ public class FileListListAdapter implements ListAdapter {
mStorageManager = storage_man;
mFiles = mStorageManager.getDirectoryContent(mFile);
mContext = context;
mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
}
@Override
@ -111,7 +116,7 @@ public class FileListListAdapter implements ListAdapter {
}
ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2);
ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);
if (file.isDownloading()) {
if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
downloaded.setVisibility(View.INVISIBLE);
downloading.setVisibility(View.VISIBLE);
} else if (file.isDown()) {

View file

@ -362,7 +362,7 @@ public class FileDetailFragment extends SherlockFragment implements
cb.setChecked(mFile.keepInSync());
// configure UI for depending upon local state of the file
if (mFile.isDownloading()) {
if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {
setButtonsForDownloading();
} else if (mFile.isDown()) {