Better failure handling in files renaming and removal
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index 7101dd0..a31541a 100644 (file)
@@ -40,6 +40,8 @@ import org.json.JSONObject;
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
+import android.annotation.SuppressLint;\r
+import android.app.Activity;\r
 import android.content.ActivityNotFoundException;\r
 import android.content.BroadcastReceiver;\r
 import android.content.Context;\r
@@ -80,6 +82,7 @@ import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.owncloud.files.services.FileDownloader;\r
 import eu.alefzero.owncloud.files.services.FileUploader;\r
+import eu.alefzero.owncloud.ui.activity.FileDetailActivity;\r
 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
 import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
 import eu.alefzero.webdav.WebdavClient;\r
@@ -97,6 +100,8 @@ public class FileDetailFragment extends SherlockFragment implements
     public static final String EXTRA_FILE = "FILE";\r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
 \r
+    private FileDetailFragment.ContainerActivity mContainerActivity;\r
+    \r
     private int mLayout;\r
     private View mView;\r
     private OCFile mFile;\r
@@ -104,6 +109,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private ImageView mPreview;\r
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
+    private UploadFinishReceiver mUploadFinishReceiver;\r
 \r
     private static final String TAG = "FileDetailFragment";\r
     public static final String FTAG = "FileDetails"; \r
@@ -140,6 +146,20 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
     \r
+\r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onAttach(Activity activity) {\r
+        super.onAttach(activity);\r
+        try {\r
+            mContainerActivity = (ContainerActivity) activity;\r
+        } catch (ClassCastException e) {\r
+            throw new ClassCastException(activity.toString() + " must implement FileListFragment.ContainerActivity");\r
+        }\r
+    }\r
+    \r
     \r
     @Override\r
     public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
@@ -183,18 +203,29 @@ public class FileDetailFragment extends SherlockFragment implements
     @Override\r
     public void onResume() {\r
         super.onResume();\r
+        \r
         mDownloadFinishReceiver = new DownloadFinishReceiver();\r
         IntentFilter filter = new IntentFilter(\r
                 FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
         getActivity().registerReceiver(mDownloadFinishReceiver, filter);\r
+        \r
+        mUploadFinishReceiver = new UploadFinishReceiver();\r
+        filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
+        getActivity().registerReceiver(mUploadFinishReceiver, filter);\r
+        \r
         mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
     }\r
 \r
     @Override\r
     public void onPause() {\r
         super.onPause();\r
+        \r
         getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
         mDownloadFinishReceiver = null;\r
+        \r
+        getActivity().unregisterReceiver(mUploadFinishReceiver);\r
+        mUploadFinishReceiver = null;\r
+        \r
         if (mPreview != null) {\r
             mPreview = null;\r
         }\r
@@ -218,10 +249,10 @@ public class FileDetailFragment extends SherlockFragment implements
                 i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
                 \r
                 // update ui \r
-                Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();\r
-                setButtonsForDownloading();\r
+                setButtonsForTransferring();\r
                 \r
                 getActivity().startService(i);\r
+                mContainerActivity.onFileStateChanged();    // this is not working; it is performed before the fileDownloadService registers it as 'in progress'\r
                 break;\r
             }\r
             case R.id.fdKeepInSync: {\r
@@ -231,6 +262,8 @@ public class FileDetailFragment extends SherlockFragment implements
                 fdsm.saveFile(mFile);\r
                 if (mFile.keepInSync()) {\r
                     onClick(getView().findViewById(R.id.fdDownloadBtn));\r
+                } else {    \r
+                    mContainerActivity.onFileStateChanged();    // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
                 }\r
                 break;\r
             }\r
@@ -241,7 +274,12 @@ public class FileDetailFragment extends SherlockFragment implements
                 break;\r
             }   \r
             case R.id.fdRemoveBtn: {\r
-                ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(R.string.confirmation_remove_alert, new String[]{mFile.getFileName()});\r
+                ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(\r
+                        R.string.confirmation_remove_alert,\r
+                        new String[]{mFile.getFileName()},\r
+                        mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,\r
+                        mFile.isDown() ? R.string.confirmation_remove_local : -1,\r
+                        R.string.common_cancel);\r
                 confDialog.setOnConfirmationListener(this);\r
                 confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);\r
                 break;\r
@@ -299,14 +337,32 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     \r
     @Override\r
-    public void onConfirmation(boolean confirmation, String callerTag) {\r
-        if (confirmation && callerTag.equals(FTAG_CONFIRMATION)) {\r
-            Log.e("ASD","onConfirmation");\r
+    public void onConfirmation(String callerTag) {\r
+        if (callerTag.equals(FTAG_CONFIRMATION)) {\r
             FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
             if (fdsm.getFileById(mFile.getFileId()) != null) {\r
                 new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start();\r
+                boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
             }\r
-        } else if (!confirmation) Log.d(TAG, "REMOVAL CANCELED");\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public void onNeutral(String callerTag) {\r
+        FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
+        File f = null;\r
+        if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {\r
+            f.delete();\r
+            mFile.setStoragePath(null);\r
+            fdsm.saveFile(mFile);\r
+            updateFileDetails(mFile, mAccount);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public void onCancel(String callerTag) {\r
+        Log.d(TAG, "REMOVAL CANCELED");\r
     }\r
     \r
     \r
@@ -362,8 +418,8 @@ public class FileDetailFragment extends SherlockFragment implements
             cb.setChecked(mFile.keepInSync());\r
 \r
             // configure UI for depending upon local state of the file\r
-            if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {\r
-                setButtonsForDownloading();\r
+            if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {\r
+                setButtonsForTransferring();\r
                 \r
             } else if (mFile.isDown()) {\r
                 // Update preview\r
@@ -374,51 +430,6 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
                 setButtonsForDown();\r
                 \r
-                // Change download button to open button\r
-                /*downloadButton.setText(R.string.filedetails_open);\r
-                downloadButton.setOnClickListener(new OnClickListener() {\r
-                    @Override\r
-                    public void onClick(View v) {\r
-                        String storagePath = mFile.getStoragePath();\r
-                        String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
-                        try {\r
-                            Intent i = new Intent(Intent.ACTION_VIEW);\r
-                            i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());\r
-                            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
-                            startActivity(i);\r
-                            \r
-                        } catch (Throwable t) {\r
-                            Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());\r
-                            boolean toastIt = true; \r
-                            String mimeType = "";\r
-                            try {\r
-                                Intent i = new Intent(Intent.ACTION_VIEW);\r
-                                mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
-                                if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {\r
-                                    i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
-                                    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
-                                    startActivity(i);\r
-                                    toastIt = false;\r
-                                }\r
-                                \r
-                            } catch (IndexOutOfBoundsException e) {\r
-                                Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);\r
-                                \r
-                            } catch (ActivityNotFoundException e) {\r
-                                Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");\r
-                                \r
-                            } catch (Throwable th) {\r
-                                Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);\r
-                                \r
-                            } finally {\r
-                                if (toastIt) {\r
-                                    Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
-                                }\r
-                            }\r
-                            \r
-                        }\r
-                    }\r
-                });*/\r
             } else {\r
                 setButtonsForRemote();\r
             }\r
@@ -484,10 +495,10 @@ public class FileDetailFragment extends SherlockFragment implements
     /**\r
      * Enables or disables buttons for a file being downloaded\r
      */\r
-    private void setButtonsForDownloading() {\r
+    private void setButtonsForTransferring() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setText(R.string.filedetails_download_in_progress);\r
+            //downloadButton.setText(R.string.filedetails_download_in_progress);    // ugly\r
             downloadButton.setEnabled(false);   // TODO replace it with a 'cancel download' button\r
         \r
             // let's protect the user from himself ;)\r
@@ -503,7 +514,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setButtonsForDown() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setText(R.string.filedetails_redownload);\r
+            //downloadButton.setText(R.string.filedetails_redownload);      // ugly\r
             downloadButton.setEnabled(true);\r
         \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);\r
@@ -518,7 +529,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setButtonsForRemote() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setText(R.string.filedetails_download);\r
+            //downloadButton.setText(R.string.filedetails_download);    // unnecessary\r
             downloadButton.setEnabled(true);\r
             \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);\r
@@ -546,6 +557,31 @@ public class FileDetailFragment extends SherlockFragment implements
         }*/\r
         return false;\r
     }\r
+    \r
+    \r
+    /**\r
+     * Interface to implement by any Activity that includes some instance of FileDetailFragment\r
+     * \r
+     * @author David A. Velasco\r
+     */\r
+    public interface ContainerActivity {\r
+\r
+        /**\r
+         * Callback method invoked when the detail fragment wants to notice its container \r
+         * activity about a relevant state the file shown by the fragment.\r
+         * \r
+         * Added to notify to FileDisplayActivity about the need of refresh the files list. \r
+         * \r
+         * Currently called when:\r
+         *  - a download is started;\r
+         *  - a rename is completed;\r
+         *  - a deletion is completed;\r
+         *  - the 'inSync' flag is changed;\r
+         */\r
+        public void onFileStateChanged();\r
+        \r
+    }\r
+    \r
 \r
     /**\r
      * Once the file download has finished -> update view\r
@@ -556,12 +592,12 @@ public class FileDetailFragment extends SherlockFragment implements
         public void onReceive(Context context, Intent intent) {\r
             String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
 \r
-            if (accountName.equals(mAccount.name) && mFile != null) {\r
+            if (!isEmpty() && accountName.equals(mAccount.name)) {\r
                 boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);\r
                 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
                 if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
                     if (downloadWasFine) {\r
-                        mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
+                        mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));    // updates the local object without accessing the database again\r
                     }\r
                     updateFileDetails();    // it updates the buttons; must be called although !downloadWasFine\r
                 }\r
@@ -569,6 +605,37 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
     \r
+    \r
+    /**\r
+     * Once the file upload has finished -> update view\r
+     * \r
+     * Being notified about the finish of an upload is necessary for the next sequence:\r
+     *   1. Upload a big file.\r
+     *   2. Force a synchronization; if it finished before the upload, the file in transfer will be included in the local database and in the file list\r
+     *      of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload. \r
+     *   3. Click the file in the list to see its details.\r
+     *   4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.\r
+     */\r
+    private class UploadFinishReceiver extends BroadcastReceiver {\r
+        @Override\r
+        public void onReceive(Context context, Intent intent) {\r
+            String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);\r
+\r
+            if (!isEmpty() && accountName.equals(mAccount.name)) {\r
+                boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);\r
+                String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);\r
+                if (mFile.getRemotePath().equals(uploadRemotePath)) {\r
+                    if (uploadWasFine) {\r
+                        FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
+                        mFile = fdsm.getFileByPath(mFile.getRemotePath());\r
+                    }\r
+                    updateFileDetails();    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
+                }\r
+            }\r
+        }\r
+    }\r
+    \r
+\r
     // this is a temporary class for sharing purposes, it need to be replaced in transfer service\r
     private class ShareRunnable implements Runnable {\r
         private String mPath;\r
@@ -700,15 +767,24 @@ public class FileDetailFragment extends SherlockFragment implements
                         newFile.setMimetype(mFile.getMimetype());\r
                         newFile.setModificationTimestamp(mFile.getModificationTimestamp());\r
                         newFile.setParentId(mFile.getParentId());\r
+                        boolean localRenameFails = false;\r
                         if (mFile.isDown()) {\r
                             File f = new File(mFile.getStoragePath());\r
                             Log.e(TAG, f.getAbsolutePath());\r
-                            f.renameTo(new File(f.getParent() + File.separator + newFilename)); // TODO check if fails\r
+                            localRenameFails = !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));\r
                             Log.e(TAG, f.getParent() + File.separator + newFilename);\r
                             newFile.setStoragePath(f.getParent() + File.separator + newFilename);\r
                         }\r
                         \r
-                        new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();\r
+                        if (localRenameFails) {\r
+                            Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG); \r
+                            msg.show();\r
+                            \r
+                        } else {\r
+                            new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();\r
+                            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                            getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                        }\r
 \r
                     }\r
                 }\r
@@ -734,6 +810,7 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
         public void run() {\r
             WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
+            wc.allowSelfsignedCertificates();\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -744,26 +821,52 @@ public class FileDetailFragment extends SherlockFragment implements
             LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),\r
                                              Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));\r
             \r
+            boolean success = false;\r
             try {\r
                 int status = wc.executeMethod(move);\r
-                if (move.succeeded()) {\r
-                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-                    fdsm.removeFile(mOld);\r
-                    fdsm.saveFile(mNew);\r
-                    mFile = mNew;\r
-                    mHandler.post(new Runnable() {\r
-                        @Override\r
-                        public void run() { updateFileDetails(mFile, mAccount); }\r
-                    });\r
-                }\r
-                Log.e("ASD", ""+move.getQueryString());\r
-                Log.d("move", "returned status " + status);\r
+                success = move.succeeded();\r
+                Log.d(TAG, "Move returned status: " + status);\r
+                \r
             } catch (HttpException e) {\r
-                // TODO Auto-generated catch block\r
-                e.printStackTrace();\r
+                Log.e(TAG, "HTTP Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
+                \r
             } catch (IOException e) {\r
-                // TODO Auto-generated catch block\r
-                e.printStackTrace();\r
+                Log.e(TAG, "I/O Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
+                \r
+            } catch (Exception e) {\r
+                Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
+            }\r
+            \r
+            if (success) {\r
+                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
+                fdsm.removeFile(mOld);\r
+                fdsm.saveFile(mNew);\r
+                mFile = mNew;\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() { \r
+                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                        updateFileDetails(mFile, mAccount);\r
+                        mContainerActivity.onFileStateChanged();\r
+                    }\r
+                });\r
+                \r
+            } else {\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                        try {\r
+                            Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG); \r
+                            msg.show();\r
+                            \r
+                        } catch (NotFoundException e) {\r
+                            e.printStackTrace();\r
+                        }\r
+                    }\r
+                });\r
             }\r
         }\r
         private class LocalMoveMethod extends DavMethodBase {\r
@@ -849,9 +952,6 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     private class RemoveRunnable implements Runnable {\r
         \r
-        /** Arbitrary timeout for deletion */\r
-        public final static int DELETION_TIMEOUT = 5000;\r
-        \r
         Account mAccount;\r
         OCFile mFileToRemove;\r
         Handler mHandler;\r
@@ -864,6 +964,7 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
         public void run() {\r
             WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
+            wc.allowSelfsignedCertificates();\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -873,65 +974,73 @@ public class FileDetailFragment extends SherlockFragment implements
             DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
             \r
             boolean success = false;\r
+            int status = -1;\r
             try {\r
-                int status = wc.executeMethod(delete, DELETION_TIMEOUT);\r
-                if (delete.succeeded()) {\r
-                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-                    fdsm.removeFile(mFileToRemove);\r
-                    mHandler.post(new Runnable() {\r
-                        @Override\r
-                        public void run() { \r
-                            try {\r
-                                Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);\r
-                                msg.show();\r
-                                if (getActivity() instanceof FileDisplayActivity) {\r
-                                    // double pane\r
-                                    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();\r
-                                    transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
-                                    transaction.commit();\r
-                                    \r
-                                } else {\r
-                                    getActivity().finish();\r
-                                }\r
-                                \r
-                            } catch (NotFoundException e) {\r
-                                e.printStackTrace();\r
-                            }\r
-                        }\r
-                    });\r
-                    success = true;\r
-                }\r
-                Log.e("ASD", ""+ delete.getQueryString());\r
-                Log.d("delete", "returned status " + status);\r
+                status = wc.executeMethod(delete);\r
+                success = (delete.succeeded());\r
+                Log.d(TAG, "Delete: returned status " + status);\r
                 \r
             } catch (HttpException e) {\r
-                e.printStackTrace();\r
+                Log.e(TAG, "HTTP Exception removing file " + mFileToRemove.getRemotePath(), e);\r
                 \r
             } catch (IOException e) {\r
-                e.printStackTrace();\r
+                Log.e(TAG, "I/O Exception removing file " + mFileToRemove.getRemotePath(), e);\r
                 \r
-            } finally {\r
-                if (!success) {\r
-                    mHandler.post(new Runnable() {\r
-                        @Override\r
-                        public void run() {\r
-                            try {\r
-                                Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); \r
-                                msg.show();\r
-                            \r
-                            } catch (NotFoundException e) {\r
-                                e.printStackTrace();\r
+            } catch (Exception e) {\r
+                Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);\r
+            }\r
+            \r
+            if (success) {\r
+                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
+                fdsm.removeFile(mFileToRemove);\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                        try {\r
+                            Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);\r
+                            msg.show();\r
+                            if (inDisplayActivity) {\r
+                                // double pane\r
+                                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();\r
+                                transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
+                                transaction.commit();\r
+                                mContainerActivity.onFileStateChanged();\r
+                                \r
+                            } else {\r
+                                getActivity().finish();\r
                             }\r
+                            \r
+                        } catch (NotFoundException e) {\r
+                            e.printStackTrace();\r
                         }\r
-                    });\r
-                }\r
+                    }\r
+                });\r
+                \r
+            } else {\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                        try {\r
+                            Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); \r
+                            msg.show();\r
+                            \r
+                        } catch (NotFoundException e) {\r
+                            e.printStackTrace();\r
+                        }\r
+                    }\r
+                });\r
             }\r
         }\r
         \r
     }\r
     \r
     class BitmapLoader extends AsyncTask<String, Void, Bitmap> {\r
-        @Override\r
+        @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20\r
+               @Override\r
         protected Bitmap doInBackground(String... params) {\r
             Bitmap result = null;\r
             if (params.length != 1) return result;\r
@@ -955,7 +1064,6 @@ public class FileDetailFragment extends SherlockFragment implements
                 int width = options.outWidth;\r
                 int height = options.outHeight;\r
                 int scale = 1;\r
-                boolean recycle = false;\r
                 if (width >= 2048 || height >= 2048) {\r
                     scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));\r
                     options.inSampleSize = scale;\r