Merge branch 'long_press'
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index 1f34ae5..4a5b5cb 100644 (file)
@@ -74,7 +74,13 @@ import com.owncloud.android.files.services.FileObserverService;
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
+import com.owncloud.android.operations.OnRemoteOperationListener;\r
+import com.owncloud.android.operations.RemoteOperation;\r
 import com.owncloud.android.operations.RemoteOperationResult;\r
 import com.owncloud.android.operations.RemoteOperationResult;\r
+import com.owncloud.android.operations.RemoveFileOperation;\r
+import com.owncloud.android.operations.RenameFileOperation;\r
+import com.owncloud.android.operations.SynchronizeFileOperation;\r
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
 import com.owncloud.android.syncadapter.FileSyncService;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
 import com.owncloud.android.syncadapter.FileSyncService;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
@@ -92,7 +98,7 @@ import eu.alefzero.webdav.WebdavClient;
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
-    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener {\r
+    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {\r
     \r
     private ArrayAdapter<String> mDirectories;\r
     private OCFile mCurrentDir = null;\r
     \r
     private ArrayAdapter<String> mDirectories;\r
     private OCFile mCurrentDir = null;\r
@@ -332,7 +338,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             default:\r
                 break;\r
             }\r
             default:\r
-                retval = false;\r
+                retval = super.onOptionsItemSelected(item);\r
         }\r
         return retval;\r
     }\r
         }\r
         return retval;\r
     }\r
@@ -896,16 +902,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
          */\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
          */\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
             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
                 OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
                 if (fileListFragment != null) { \r
                     fileListFragment.listDirectory();\r
@@ -924,9 +925,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
         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
-\r
-            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) &&\r
-                     mCurrentDir != null && mCurrentDir.getFileId() == mStorageManager.getFileByPath(downloadedRemotePath).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
                 OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
                 if (fileListFragment != null) { \r
                     fileListFragment.listDirectory();\r
@@ -1051,7 +1052,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             if (mDualPane) {\r
                 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
                 if (fragment != null)\r
             if (mDualPane) {\r
                 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
                 if (fragment != null)\r
-                    fragment.updateFileDetails();\r
+                    fragment.updateFileDetails(false);\r
             }\r
         }\r
 \r
             }\r
         }\r
 \r
@@ -1096,4 +1097,151 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
 \r
     }\r
 \r
 \r
+    /**\r
+     * Updates the view associated to the activity after the finish of some operation over files\r
+     * in the current account.\r
+     * \r
+     * @param operation     Removal operation performed.\r
+     * @param result        Result of the removal.\r
+     */\r
+    @Override\r
+    public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
+        if (operation instanceof RemoveFileOperation) {\r
+            onRemoveFileOperationFinish((RemoveFileOperation)operation, result);\r
+                \r
+        } else if (operation instanceof RenameFileOperation) {\r
+            onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
+            \r
+        } else if (operation instanceof SynchronizeFileOperation) {\r
+            onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Updates the view associated to the activity after the finish of an operation trying to remove a \r
+     * file. \r
+     * \r
+     * @param operation     Removal operation performed.\r
+     * @param result        Result of the removal.\r
+     */\r
+    private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {\r
+        dismissDialog(DIALOG_SHORT_WAIT);\r
+        if (result.isSuccess()) {\r
+            Toast msg = Toast.makeText(this, R.string.remove_success_msg, Toast.LENGTH_LONG);\r
+            msg.show();\r
+            OCFile removedFile = operation.getFile();\r
+            if (mDualPane) {\r
+                FileDetailFragment details = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+                if (details != null && removedFile.equals(details.getDisplayedFile()) ) {\r
+                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                    transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
+                    transaction.commit();\r
+                }\r
+            }\r
+            if (mStorageManager.getFileById(removedFile.getParentId()).equals(mCurrentDir)) {\r
+                mFileList.listDirectory();\r
+            }\r
+                \r
+        } else {\r
+            Toast msg = Toast.makeText(this, R.string.remove_fail_msg, Toast.LENGTH_LONG); \r
+            msg.show();\r
+            if (result.isSslRecoverableException()) {\r
+                mLastSslUntrustedServerResult = result;\r
+                showDialog(DIALOG_SSL_VALIDATOR); \r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Updates the view associated to the activity after the finish of an operation trying to rename a \r
+     * file. \r
+     * \r
+     * @param operation     Renaming operation performed.\r
+     * @param result        Result of the renaming.\r
+     */\r
+    private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {\r
+        dismissDialog(DIALOG_SHORT_WAIT);\r
+        OCFile renamedFile = operation.getFile();\r
+        if (result.isSuccess()) {\r
+            if (mDualPane) {\r
+                FileDetailFragment details = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+                if (details != null && renamedFile.equals(details.getDisplayedFile()) ) {\r
+                    details.updateFileDetails(renamedFile, AccountUtils.getCurrentOwnCloudAccount(this));\r
+                }\r
+            }\r
+            if (mStorageManager.getFileById(renamedFile.getParentId()).equals(mCurrentDir)) {\r
+                mFileList.listDirectory();\r
+            }\r
+            \r
+        } else {\r
+            if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {\r
+                Toast msg = Toast.makeText(this, R.string.rename_local_fail_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+                // TODO throw again the new rename dialog\r
+            } else {\r
+                Toast msg = Toast.makeText(this, R.string.rename_server_fail_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+                if (result.isSslRecoverableException()) {\r
+                    mLastSslUntrustedServerResult = result;\r
+                    showDialog(DIALOG_SSL_VALIDATOR); \r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {\r
+        dismissDialog(DIALOG_SHORT_WAIT);\r
+        OCFile syncedFile = operation.getLocalFile();\r
+        if (!result.isSuccess()) {\r
+            if (result.getCode() == ResultCode.SYNC_CONFLICT) {\r
+                Intent i = new Intent(this, ConflictsResolveActivity.class);\r
+                i.putExtra(ConflictsResolveActivity.EXTRA_FILE, syncedFile);\r
+                i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));\r
+                startActivity(i);\r
+                \r
+            } else {\r
+                Toast msg = Toast.makeText(this, R.string.sync_file_fail_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+            }\r
+            \r
+        } else {\r
+            if (operation.transferWasRequested()) {\r
+                mFileList.listDirectory();\r
+                onTransferStateChanged(syncedFile, true, true);\r
+                \r
+            } else {\r
+                Toast msg = Toast.makeText(this, R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {\r
+        /*OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+        if (fileListFragment != null) { \r
+            fileListFragment.listDirectory();\r
+        }*/\r
+        if (mDualPane) {\r
+            FileDetailFragment details = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+            if (details != null && file.equals(details.getDisplayedFile()) ) {\r
+                if (downloading || uploading) {\r
+                    details.updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));\r
+                } else {\r
+                    details.updateFileDetails(downloading || uploading);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    \r
+\r
+\r
 }\r
 }\r