Browse Source

Refactored remove file operation (inherits RemoteOperation, so generates RemoteOperationResult)

David A. Velasco 12 years ago
parent
commit
900563e245

+ 90 - 0
src/com/owncloud/android/operations/RemoveFileOperation.java

@@ -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;
+    }
+    
+}

+ 54 - 94
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -33,7 +33,6 @@ import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.protocol.HTTP;
 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
-import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
 import org.json.JSONObject;
 
@@ -83,6 +82,10 @@ import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.network.OwnCloudClientUtils;
+import com.owncloud.android.operations.OnRemoteOperationListener;
+import com.owncloud.android.operations.RemoteOperation;
+import com.owncloud.android.operations.RemoteOperationResult;
+import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.ui.activity.FileDetailActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.TransferServiceGetter;
@@ -99,7 +102,7 @@ import eu.alefzero.webdav.WebdavUtils;
  * 
  */
 public class FileDetailFragment extends SherlockFragment implements
-        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
+        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {
 
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
@@ -114,6 +117,9 @@ public class FileDetailFragment extends SherlockFragment implements
     
     private DownloadFinishReceiver mDownloadFinishReceiver;
     private UploadFinishReceiver mUploadFinishReceiver;
+    
+    private Handler mHandler;
+    private RemoteOperation mLastRemoteOperation;
 
     private static final String TAG = "FileDetailFragment";
     public static final String FTAG = "FileDetails"; 
@@ -150,6 +156,13 @@ public class FileDetailFragment extends SherlockFragment implements
         }
     }
     
+    
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mHandler = new Handler();
+    }
+    
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -390,7 +403,12 @@ public class FileDetailFragment extends SherlockFragment implements
         if (callerTag.equals(FTAG_CONFIRMATION)) {
             FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
             if (fdsm.getFileById(mFile.getFileId()) != null) {
-                new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start();
+                mLastRemoteOperation = new RemoveFileOperation( mFile, 
+                                                                true, 
+                                                                new FileDataStorageManager(mAccount, getActivity().getContentResolver()));
+                WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
+                mLastRemoteOperation.execute(wc, this, mHandler);
+                
                 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
                 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
             }
@@ -1007,97 +1025,6 @@ public class FileDetailFragment extends SherlockFragment implements
         
     }
     
-    private class RemoveRunnable implements Runnable {
-        
-        Account mAccount;
-        OCFile mFileToRemove;
-        Handler mHandler;
-        
-        public RemoveRunnable(OCFile fileToRemove, Account account, Handler handler) {
-            mFileToRemove = fileToRemove;
-            mAccount = account;
-            mHandler = handler;
-        }
-        
-        public void run() {
-            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
-            AccountManager am = AccountManager.get(getSherlockActivity());
-            String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
-            OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
-            String webdav_path = AccountUtils.getWebdavPath(ocv);
-            Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
-
-            DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
-            
-            boolean success = false;
-            int status = -1;
-            try {
-                status = wc.executeMethod(delete);
-                success = (delete.succeeded());
-                delete.getResponseBodyAsString();   // exhaust the response, although not interesting
-                Log.d(TAG, "Delete: returned status " + status);
-                
-            } catch (HttpException e) {
-                Log.e(TAG, "HTTP Exception removing file " + mFileToRemove.getRemotePath(), e);
-                
-            } catch (IOException e) {
-                Log.e(TAG, "I/O Exception removing file " + mFileToRemove.getRemotePath(), e);
-                
-            } catch (Exception e) {
-                Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);
-                
-            } finally {
-                delete.releaseConnection();
-            }
-            
-            if (success) {
-                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                fdsm.removeFile(mFileToRemove, true);
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                        try {
-                            Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
-                            msg.show();
-                            if (inDisplayActivity) {
-                                // double pane
-                                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
-                                transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
-                                transaction.commit();
-                                mContainerActivity.onFileStateChanged();
-                                
-                            } else {
-                                getActivity().finish();
-                            }
-                            
-                        } catch (NotFoundException e) {
-                            e.printStackTrace();
-                        }
-                    }
-                });
-                
-            } else {
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                        try {
-                            Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); 
-                            msg.show();
-                            
-                        } catch (NotFoundException e) {
-                            e.printStackTrace();
-                        }
-                    }
-                });
-            }
-        }
-        
-    }
-    
     class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
         @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
 		@Override
@@ -1171,6 +1098,39 @@ public class FileDetailFragment extends SherlockFragment implements
         }
         
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
+        if (operation.equals(mLastRemoteOperation)) {
+            if (operation instanceof RemoveFileOperation) {
+                boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+                getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
+                if (result.isSuccess()) {
+                    Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
+                    msg.show();
+                    if (inDisplayActivity) {
+                        // double pane
+                        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
+                        transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
+                        transaction.commit();
+                        mContainerActivity.onFileStateChanged();
+                    } else {
+                        getActivity().finish();
+                    }
+                    
+                } else {
+                    Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); 
+                    msg.show();
+                    if (result.isSslRecoverableException()) {
+                        // TODO
+                    }
+                }
+            }
+        }
+    }
     
 
 }