Added a static list of downloads in progress to have a reliable way to check if a...
authorDavid A. Velasco <dvelasco@solidgear.es>
Tue, 24 Jul 2012 12:08:54 +0000 (14:08 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Tue, 24 Jul 2012 12:08:54 +0000 (14:08 +0200)
AndroidManifest.xml
src/eu/alefzero/owncloud/datamodel/OCFile.java
src/eu/alefzero/owncloud/files/services/FileDownloader.java
src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

index 22505c8..9a269f2 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.177B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.178B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 7284604..b23abf9 100644 (file)
@@ -157,20 +157,6 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
     
     /**
-     * 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
      * 
      * @return The local path to the file
index e5316b3..c147c9d 100644 (file)
@@ -2,6 +2,9 @@ package eu.alefzero.owncloud.files.services;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -48,7 +51,27 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private long mTotalDownloadSize;\r
     private long mCurrentDownlodSize;\r
     private Notification mNotification;\r
+    \r
+    /**\r
+     * Static map with the files being download and the path to the temporal file were are download\r
+     */\r
+    private static Map<String, String> mDownloadsInProgress = Collections.synchronizedMap(new HashMap<String, String>());\r
+    \r
+    /**\r
+     * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading\r
+     */\r
+    public static boolean isDownloading(Account account, String remotePath) {\r
+        return (mDownloadsInProgress.get(buildRemoteName(account.name, remotePath)) != null);\r
+    }\r
+    \r
+    /**\r
+     * Builds a key for mDownloadsInProgress from the accountName and remotePath\r
+     */\r
+    private static String buildRemoteName(String accountName, String remotePath) {\r
+        return accountName + remotePath;\r
+    }\r
 \r
+    \r
     private final class ServiceHandler extends Handler {\r
         public ServiceHandler(Looper looper) {\r
             super(looper);\r
@@ -141,6 +164,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         // download in a temporal file\r
         File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath);\r
         tmpFile.getParentFile().mkdirs();\r
+        mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath());\r
 \r
         boolean download_result = false;\r
         File newFile = null;\r
@@ -164,6 +188,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             }\r
         }\r
         \r
+        mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath));\r
+        \r
         mNotificationMngr.cancel(1);\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
         end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);\r
index 9c79603..bcdce79 100644 (file)
@@ -19,11 +19,14 @@ package eu.alefzero.owncloud.ui.adapter;
 \r
 import java.util.Vector;\r
 \r
+import eu.alefzero.owncloud.AccountUtils;\r
 import eu.alefzero.owncloud.DisplayUtils;\r
 import eu.alefzero.owncloud.R;\r
 import eu.alefzero.owncloud.datamodel.DataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
+import eu.alefzero.owncloud.files.services.FileDownloader;\r
 \r
+import android.accounts.Account;\r
 import android.content.Context;\r
 import android.database.DataSetObserver;\r
 import android.util.Log;\r
@@ -46,6 +49,7 @@ public class FileListListAdapter implements ListAdapter {
     private OCFile mFile;\r
     private Vector<OCFile> mFiles;\r
     private DataStorageManager mStorageManager;\r
+    private Account mAccount;\r
 \r
     public FileListListAdapter(OCFile file, DataStorageManager storage_man,\r
             Context context) {\r
@@ -53,6 +57,7 @@ public class FileListListAdapter implements ListAdapter {
         mStorageManager = storage_man;\r
         mFiles = mStorageManager.getDirectoryContent(mFile);\r
         mContext = context;\r
+        mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);\r
     }\r
 \r
     @Override\r
@@ -111,7 +116,7 @@ public class FileListListAdapter implements ListAdapter {
             }\r
             ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2);\r
             ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);\r
-            if (file.isDownloading()) {\r
+            if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {\r
                 downloaded.setVisibility(View.INVISIBLE);\r
                 downloading.setVisibility(View.VISIBLE);\r
             } else if (file.isDown()) {\r
index ed98635..7101dd0 100644 (file)
@@ -362,7 +362,7 @@ public class FileDetailFragment extends SherlockFragment implements
             cb.setChecked(mFile.keepInSync());\r
 \r
             // configure UI for depending upon local state of the file\r
-            if (mFile.isDownloading()) {\r
+            if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {\r
                 setButtonsForDownloading();\r
                 \r
             } else if (mFile.isDown()) {\r