Refactored remove file operation (inherits RemoteOperation, so generates RemoteOperat...
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 25 Oct 2012 12:21:01 +0000 (14:21 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 25 Oct 2012 12:21:01 +0000 (14:21 +0200)
src/com/owncloud/android/operations/RemoveFileOperation.java [new file with mode: 0644]
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

diff --git a/src/com/owncloud/android/operations/RemoveFileOperation.java b/src/com/owncloud/android/operations/RemoveFileOperation.java
new file mode 100644 (file)
index 0000000..aa57356
--- /dev/null
@@ -0,0 +1,90 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012 Bartek Przybylski
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.operations;
+
+import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
+
+import android.util.Log;
+
+import com.owncloud.android.datamodel.DataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+
+import eu.alefzero.webdav.WebdavClient;
+import eu.alefzero.webdav.WebdavUtils;
+
+/**
+ * Remote operation performing the removal of a remote file or folder in the ownCloud server.
+ * 
+ * @author David A. Velasco
+ */
+public class RemoveFileOperation extends RemoteOperation {
+    
+    private static final String TAG = RemoveFileOperation.class.getCanonicalName();
+
+    private static final int REMOVE_READ_TIMEOUT = 10000;
+    private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
+    
+    OCFile mFileToRemove;
+    boolean mDeleteLocalCopy;
+    DataStorageManager mDataStorageManager;
+    
+    
+    /**
+     * Constructor
+     * 
+     * @param fileToRemove          OCFile instance describing the remote file or folder to remove from the server
+     * @param deleteLocalCopy       When 'true', and a local copy of the file exists, it is also removed.
+     * @param storageManager        Reference to the local database corresponding to the account where the file is contained. 
+     */
+    public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, DataStorageManager storageManager) {
+        mFileToRemove = fileToRemove;
+        mDeleteLocalCopy = deleteLocalCopy;
+        mDataStorageManager = storageManager;
+    }
+    
+    
+    /**
+     * Performs the removal
+     */
+    @Override
+    protected RemoteOperationResult run(WebdavClient client) {
+        RemoteOperationResult result = null;
+        DeleteMethod delete = null;
+        try {
+            delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
+            int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
+            if (delete.succeeded()) {
+                mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
+            }
+            delete.getResponseBodyAsString();   // exhaust the response, although not interesting
+            result = new RemoteOperationResult(delete.succeeded(), status);
+            Log.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
+            
+        } catch (Exception e) {
+            result = new RemoteOperationResult(e);
+            Log.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
+            
+        } finally {
+            if (delete != null)
+                delete.releaseConnection();
+        }
+        return result;
+    }
+    
+}
index 3f0be07..ef76a54 100644 (file)
@@ -33,7 +33,6 @@ import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.http.message.BasicNameValuePair;\r
 import org.apache.http.protocol.HTTP;\r
 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;\r
 import org.apache.http.message.BasicNameValuePair;\r
 import org.apache.http.protocol.HTTP;\r
 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;\r
-import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
 import org.json.JSONObject;\r
 \r
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
 import org.json.JSONObject;\r
 \r
@@ -83,6 +82,10 @@ import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\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.RemoveFileOperation;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.activity.FileDisplayActivity;\r
 import com.owncloud.android.ui.activity.TransferServiceGetter;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.activity.FileDisplayActivity;\r
 import com.owncloud.android.ui.activity.TransferServiceGetter;\r
@@ -99,7 +102,7 @@ import eu.alefzero.webdav.WebdavUtils;
  * \r
  */\r
 public class FileDetailFragment extends SherlockFragment implements\r
  * \r
  */\r
 public class FileDetailFragment extends SherlockFragment implements\r
-        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener {\r
+        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {\r
 \r
     public static final String EXTRA_FILE = "FILE";\r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
 \r
     public static final String EXTRA_FILE = "FILE";\r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
@@ -114,6 +117,9 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
     private UploadFinishReceiver mUploadFinishReceiver;\r
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
     private UploadFinishReceiver mUploadFinishReceiver;\r
+    \r
+    private Handler mHandler;\r
+    private RemoteOperation mLastRemoteOperation;\r
 \r
     private static final String TAG = "FileDetailFragment";\r
     public static final String FTAG = "FileDetails"; \r
 \r
     private static final String TAG = "FileDetailFragment";\r
     public static final String FTAG = "FileDetails"; \r
@@ -150,6 +156,13 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
     \r
         }\r
     }\r
     \r
+    \r
+    @Override\r
+    public void onCreate(Bundle savedInstanceState) {\r
+        super.onCreate(savedInstanceState);\r
+        mHandler = new Handler();\r
+    }\r
+    \r
 \r
     @Override\r
     public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
 \r
     @Override\r
     public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
@@ -390,7 +403,12 @@ public class FileDetailFragment extends SherlockFragment implements
         if (callerTag.equals(FTAG_CONFIRMATION)) {\r
             FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
             if (fdsm.getFileById(mFile.getFileId()) != null) {\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
+                mLastRemoteOperation = new RemoveFileOperation( mFile, \r
+                                                                true, \r
+                                                                new FileDataStorageManager(mAccount, getActivity().getContentResolver()));\r
+                WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
+                mLastRemoteOperation.execute(wc, this, mHandler);\r
+                \r
                 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
                 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
             }\r
                 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
                 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
             }\r
@@ -1007,97 +1025,6 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
     }\r
     \r
         \r
     }\r
     \r
-    private class RemoveRunnable implements Runnable {\r
-        \r
-        Account mAccount;\r
-        OCFile mFileToRemove;\r
-        Handler mHandler;\r
-        \r
-        public RemoveRunnable(OCFile fileToRemove, Account account, Handler handler) {\r
-            mFileToRemove = fileToRemove;\r
-            mAccount = account;\r
-            mHandler = handler;\r
-        }\r
-        \r
-        public void run() {\r
-            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\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
-            String webdav_path = AccountUtils.getWebdavPath(ocv);\r
-            Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
-\r
-            DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
-            \r
-            boolean success = false;\r
-            int status = -1;\r
-            try {\r
-                status = wc.executeMethod(delete);\r
-                success = (delete.succeeded());\r
-                delete.getResponseBodyAsString();   // exhaust the response, although not interesting\r
-                Log.d(TAG, "Delete: returned status " + status);\r
-                \r
-            } catch (HttpException e) {\r
-                Log.e(TAG, "HTTP Exception removing file " + mFileToRemove.getRemotePath(), e);\r
-                \r
-            } catch (IOException e) {\r
-                Log.e(TAG, "I/O Exception removing file " + mFileToRemove.getRemotePath(), e);\r
-                \r
-            } catch (Exception e) {\r
-                Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);\r
-                \r
-            } finally {\r
-                delete.releaseConnection();\r
-            }\r
-            \r
-            if (success) {\r
-                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-                fdsm.removeFile(mFileToRemove, true);\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
-            } 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
         @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20\r
                @Override\r
     class BitmapLoader extends AsyncTask<String, Void, Bitmap> {\r
         @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20\r
                @Override\r
@@ -1171,6 +1098,39 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
         \r
     }\r
         }\r
         \r
     }\r
+\r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
+        if (operation.equals(mLastRemoteOperation)) {\r
+            if (operation instanceof RemoveFileOperation) {\r
+                boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                if (result.isSuccess()) {\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
+                    } else {\r
+                        getActivity().finish();\r
+                    }\r
+                    \r
+                } else {\r
+                    Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); \r
+                    msg.show();\r
+                    if (result.isSslRecoverableException()) {\r
+                        // TODO\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
     \r
 \r
 }\r
     \r
 \r
 }\r