Disable rename and remove actions on folders when some file is uploading or downloadi...
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 22 Nov 2012 12:06:54 +0000 (13:06 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 22 Nov 2012 12:06:54 +0000 (13:06 +0100)
src/com/owncloud/android/files/services/FileDownloader.java
src/com/owncloud/android/files/services/FileUploader.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/adapter/FileListListAdapter.java
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

index 1ac3a0a..0f2d674 100644 (file)
@@ -202,14 +202,27 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         \r
         \r
         /**\r
-         * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download\r
+         * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download.\r
+         * \r
+         * If 'file' is a directory, returns 'true' if some of its descendant files is downloading or waiting to download. \r
          * \r
          * @param account       Owncloud account where the remote file is stored.\r
          * @param file          A file that could be in the queue of downloads.\r
          */\r
         public boolean isDownloading(Account account, OCFile file) {\r
+            String targetKey = buildRemoteName(account, file);\r
             synchronized (mPendingDownloads) {\r
-                return (mPendingDownloads.containsKey(buildRemoteName(account, file)));\r
+                if (file.isDirectory()) {\r
+                    // this can be slow if there are many downloads :(\r
+                    Iterator<String> it = mPendingDownloads.keySet().iterator();\r
+                    boolean found = false;\r
+                    while (it.hasNext() && !found) {\r
+                        found = it.next().startsWith(targetKey);\r
+                    }\r
+                    return found;\r
+                } else {\r
+                    return (mPendingDownloads.containsKey(targetKey));\r
+                }\r
             }\r
         }\r
     }\r
index aef15db..7956a99 100644 (file)
@@ -64,7 +64,6 @@ import eu.alefzero.webdav.WebdavClient;
 public class FileUploader extends Service implements OnDatatransferProgressListener {
 
     public static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
-    public static final String EXTRA_PARENT_DIR_ID = "PARENT_DIR_ID";
     public static final String EXTRA_UPLOAD_RESULT = "RESULT";
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
     public static final String EXTRA_FILE_PATH = "FILE_PATH";
@@ -281,12 +280,25 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         /**
          * Returns True when the file described by 'file' is being uploaded to the ownCloud account 'account' or waiting for it
          * 
+         * 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 will be stored.
          * @param file          A file that could be in the queue of pending uploads
          */
         public boolean isUploading(Account account, OCFile file) {
+            String targetKey = buildRemoteName(account, file);
             synchronized (mPendingUploads) {
-                return (mPendingUploads.containsKey(buildRemoteName(account, file)));
+                if (file.isDirectory()) {
+                    // this can be slow if there are many downloads :(
+                    Iterator<String> it = mPendingUploads.keySet().iterator();
+                    boolean found = false;
+                    while (it.hasNext() && !found) {
+                        found = it.next().startsWith(targetKey);
+                    }
+                    return found;
+                } else {
+                    return (mPendingUploads.containsKey(targetKey));
+                }
             }
         }
     }
@@ -572,7 +584,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         end.putExtra(EXTRA_FILE_PATH, upload.getStoragePath());
         end.putExtra(ACCOUNT_NAME, upload.getAccount().name);
         end.putExtra(EXTRA_UPLOAD_RESULT, uploadResult.isSuccess());
-        end.putExtra(EXTRA_PARENT_DIR_ID, upload.getFile().getParentId());
         sendBroadcast(end);
     }
 
index 49370e5..e6771e0 100644 (file)
@@ -874,16 +874,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
          */\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
-            long parentDirId = intent.getLongExtra(FileUploader.EXTRA_PARENT_DIR_ID, -1);\r
-            OCFile parentDir = mStorageManager.getFileById(parentDirId);\r
+            String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
             String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);\r
-\r
-            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) &&\r
-                    parentDir != null && \r
-                    (   (mCurrentDir == null && parentDir.getFileName().equals("/")) ||\r
-                            parentDir.equals(mCurrentDir)\r
-                    )\r
-                ) {\r
+            boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);\r
+            boolean isDescendant = (mCurrentDir != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
+            if (sameAccount && isDescendant) {\r
                 OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
                 if (fileListFragment != null) { \r
                     fileListFragment.listDirectory();\r
@@ -902,11 +897,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         public void onReceive(Context context, Intent intent) {\r
             String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
             String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-            OCFile downloadedFile = mStorageManager.getFileByPath(downloadedRemotePath);    // if null, the file is not in the current account, OR WAS DELETED before the download finished \r
-            \r
-            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) &&\r
-                     mCurrentDir != null && downloadedFile != null &&\r
-                     mCurrentDir.getFileId() == downloadedFile.getParentId()) {\r
+            boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);\r
+            boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
+            if (sameAccount && isDescendant) {\r
                 OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
                 if (fileListFragment != null) { \r
                     fileListFragment.listDirectory();\r
index 8717289..503dfff 100644 (file)
@@ -121,7 +121,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                 fileIcon.setImageResource(R.drawable.ic_menu_archive);\r
             }\r
             ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);\r
-            //if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {\r
             FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();\r
             FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();\r
             if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {\r
index 72d530c..a96f400 100644 (file)
@@ -18,6 +18,8 @@
 package com.owncloud.android.ui.fragment;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.R;
@@ -161,36 +163,65 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial
         inflater.inflate(R.menu.file_context_menu, menu);
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
         OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
-        MenuItem item = null;
-        int[] ids = null;
+        List<Integer> toHide = new ArrayList<Integer>();    
+        List<Integer> toDisable = new ArrayList<Integer>();  
+        
         if (targetFile.isDirectory()) {
-            int[] theIds = {R.id.open_file_item, R.id.download_file_item, R.id.cancel_download_item, R.id.cancel_upload_item};
-            ids = theIds;
-            
-        } else if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
-            int[] theIds = {R.id.open_file_item, R.id.download_file_item, R.id.cancel_upload_item};
-            ids = theIds;
-            
-        } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
-            int[] theIds = {R.id.open_file_item, R.id.download_file_item, R.id.cancel_download_item};
-            ids = theIds;
-            
-        } else if ( targetFile.isDown()) {
-            int[] theIds = {R.id.cancel_download_item, R.id.cancel_upload_item};
-            ids = theIds;
+            // contextual menu for folders
+            toHide.add(R.id.open_file_item);
+            toHide.add(R.id.download_file_item);
+            toHide.add(R.id.cancel_download_item);
+            toHide.add(R.id.cancel_upload_item);
+            if (    mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ||
+                    mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)           ) {
+                toDisable.add(R.id.rename_file_item);
+                toDisable.add(R.id.remove_file_item);
+                
+            }
             
         } else {
-            int[] theIds = {R.id.open_file_item, R.id.cancel_download_item, R.id.cancel_upload_item};
-            ids = theIds;
+            // contextual menu for regular files
+            if (targetFile.isDown()) {
+                toHide.add(R.id.cancel_download_item);
+                toHide.add(R.id.cancel_upload_item);
+            } else {
+                toHide.add(R.id.open_file_item);
+            }
+            if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
+                toHide.add(R.id.download_file_item);
+                toHide.add(R.id.cancel_upload_item);
+                toDisable.add(R.id.open_file_item);
+                toDisable.add(R.id.rename_file_item);
+                toDisable.add(R.id.remove_file_item);
+                    
+            } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
+                toHide.add(R.id.download_file_item);
+                toHide.add(R.id.cancel_download_item);
+                toDisable.add(R.id.open_file_item);
+                toDisable.add(R.id.rename_file_item);
+                toDisable.add(R.id.remove_file_item);
+                    
+            } else {
+                toHide.add(R.id.cancel_download_item);
+                toHide.add(R.id.cancel_upload_item);
+            }
         }
-        
-        for (int i=0; i < ids.length; i++) {
-            item = menu.findItem(ids[i]);
+
+        MenuItem item = null;
+        for (int i : toHide) {
+            item = menu.findItem(i);
             if (item != null) {
                 item.setVisible(false);
                 item.setEnabled(false);
             }
         }
+        
+        for (int i : toDisable) {
+            item = menu.findItem(i);
+            if (item != null) {
+                item.setEnabled(false);
+            }
+        }
     }