Browse Source

Better failure handling in files renaming and removal

David A. Velasco 13 năm trước cách đây
mục cha
commit
6a9eaaf9aa

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.191B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.192B" xmlns:android="http://schemas.android.com/apk/res/android">
 
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />

+ 3 - 0
res/values/strings.xml

@@ -160,6 +160,9 @@
     <string name="remove_success_msg">"Successful removal"</string>
     <string name="remove_fail_msg">"Removal could not be completed"</string>
     
+    <string name="rename_local_fail_msg">"Local copy could not be renamed; try a differente new name"</string>
+    <string name="rename_server_fail_msg">"Rename could not be completed"</string>
+        
     <string name="create_dir_fail_msg">Directory could not be created</string>
     
     <string name="wait_a_moment">Wait a moment</string>

+ 25 - 3
src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java

@@ -18,11 +18,12 @@
 package eu.alefzero.owncloud.ui.activity;
 
 import android.accounts.Account;
+import android.app.Dialog;
+import android.app.ProgressDialog;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Bundle;
 import android.support.v4.app.FragmentTransaction;
-import android.util.Log;
 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.app.SherlockFragmentActivity;
@@ -32,7 +33,6 @@ import eu.alefzero.owncloud.R;
 import eu.alefzero.owncloud.datamodel.OCFile;
 import eu.alefzero.owncloud.files.services.FileDownloader;
 import eu.alefzero.owncloud.ui.fragment.FileDetailFragment;
-import eu.alefzero.owncloud.ui.fragment.FileListFragment;
 
 /**
  * This activity displays the details of a file like its name, its size and so
@@ -43,6 +43,8 @@ import eu.alefzero.owncloud.ui.fragment.FileListFragment;
  */
 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
     
+    public static final int DIALOG_SHORT_WAIT = 0;
+    
     private boolean mConfigurationChangedToLandscape = false;
 
     @Override
@@ -110,6 +112,27 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         finish();
     }
     
+    
+    @Override
+    protected Dialog onCreateDialog(int id) {
+        Dialog dialog = null;
+        switch (id) {
+        case DIALOG_SHORT_WAIT: {
+            ProgressDialog working_dialog = new ProgressDialog(this);
+            working_dialog.setMessage(getResources().getString(
+                    R.string.wait_a_moment));
+            working_dialog.setIndeterminate(true);
+            working_dialog.setCancelable(false);
+            dialog = working_dialog;
+            break;
+        }
+        default:
+            dialog = null;
+        }
+        return dialog;
+    }
+    
+    
     /**
      * {@inheritDoc}
      */
@@ -117,6 +140,5 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     public void onFileStateChanged() {
         // nothing to do here!
     }
-    
 
 }

+ 1 - 1
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java

@@ -107,7 +107,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private static final int DIALOG_SETUP_ACCOUNT = 0;
     private static final int DIALOG_CREATE_DIR = 1;
     private static final int DIALOG_ABOUT_APP = 2;
-    private static final int DIALOG_SHORT_WAIT = 3;
+    public static final int DIALOG_SHORT_WAIT = 3;
     
     private static final int ACTION_SELECT_FILE = 1;
     

+ 107 - 70
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

@@ -82,6 +82,7 @@ import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
 import eu.alefzero.owncloud.datamodel.OCFile;
 import eu.alefzero.owncloud.files.services.FileDownloader;
 import eu.alefzero.owncloud.files.services.FileUploader;
+import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
 import eu.alefzero.owncloud.utils.OwnCloudVersion;
 import eu.alefzero.webdav.WebdavClient;
@@ -338,10 +339,11 @@ public class FileDetailFragment extends SherlockFragment implements
     @Override
     public void onConfirmation(String callerTag) {
         if (callerTag.equals(FTAG_CONFIRMATION)) {
-            Log.e("ASD","onConfirmation");
             FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
             if (fdsm.getFileById(mFile.getFileId()) != null) {
                 new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start();
+                boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+                getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
             }
         }
     }
@@ -765,15 +767,24 @@ public class FileDetailFragment extends SherlockFragment implements
                         newFile.setMimetype(mFile.getMimetype());
                         newFile.setModificationTimestamp(mFile.getModificationTimestamp());
                         newFile.setParentId(mFile.getParentId());
+                        boolean localRenameFails = false;
                         if (mFile.isDown()) {
                             File f = new File(mFile.getStoragePath());
                             Log.e(TAG, f.getAbsolutePath());
-                            f.renameTo(new File(f.getParent() + File.separator + newFilename)); // TODO check if fails
+                            localRenameFails = !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));
                             Log.e(TAG, f.getParent() + File.separator + newFilename);
                             newFile.setStoragePath(f.getParent() + File.separator + newFilename);
                         }
                         
-                        new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
+                        if (localRenameFails) {
+                            Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG); 
+                            msg.show();
+                            
+                        } else {
+                            new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
+                            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+                            getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
+                        }
 
                     }
                 }
@@ -810,29 +821,52 @@ public class FileDetailFragment extends SherlockFragment implements
             LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),
                                              Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));
             
+            boolean success = false;
             try {
                 int status = wc.executeMethod(move);
-                if (move.succeeded()) {
-                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                    fdsm.removeFile(mOld);
-                    fdsm.saveFile(mNew);
-                    mFile = mNew;
-                    mHandler.post(new Runnable() {
-                        @Override
-                        public void run() { 
-                            updateFileDetails(mFile, mAccount);
-                            mContainerActivity.onFileStateChanged();
-                        }
-                    });
-                }
-                Log.e("ASD", ""+move.getQueryString());
-                Log.d("move", "returned status " + status);
+                success = move.succeeded();
+                Log.d(TAG, "Move returned status: " + status);
+                
             } catch (HttpException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
+                Log.e(TAG, "HTTP Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
+                
             } catch (IOException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
+                Log.e(TAG, "I/O Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
+                
+            } catch (Exception e) {
+                Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
+            }
+            
+            if (success) {
+                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
+                fdsm.removeFile(mOld);
+                fdsm.saveFile(mNew);
+                mFile = mNew;
+                mHandler.post(new Runnable() {
+                    @Override
+                    public void run() { 
+                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
+                        updateFileDetails(mFile, mAccount);
+                        mContainerActivity.onFileStateChanged();
+                    }
+                });
+                
+            } 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.rename_server_fail_msg, Toast.LENGTH_LONG); 
+                            msg.show();
+                            
+                        } catch (NotFoundException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                });
             }
         }
         private class LocalMoveMethod extends DavMethodBase {
@@ -918,9 +952,6 @@ public class FileDetailFragment extends SherlockFragment implements
     
     private class RemoveRunnable implements Runnable {
         
-        /** Arbitrary timeout for deletion */
-        public final static int DELETION_TIMEOUT = 5000;
-        
         Account mAccount;
         OCFile mFileToRemove;
         Handler mHandler;
@@ -943,59 +974,65 @@ public class FileDetailFragment extends SherlockFragment implements
             DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
             
             boolean success = false;
+            int status = -1;
             try {
-                int status = wc.executeMethod(delete, DELETION_TIMEOUT);
-                if (delete.succeeded()) {
-                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                    fdsm.removeFile(mFileToRemove);
-                    mHandler.post(new Runnable() {
-                        @Override
-                        public void run() { 
-                            try {
-                                Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
-                                msg.show();
-                                if (getActivity() instanceof FileDisplayActivity) {
-                                    // 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();
-                            }
-                        }
-                    });
-                    success = true;
-                }
-                Log.e("ASD", ""+ delete.getQueryString());
-                Log.d("delete", "returned status " + status);
+                status = wc.executeMethod(delete);
+                success = (delete.succeeded());
+                Log.d(TAG, "Delete: returned status " + status);
                 
             } catch (HttpException e) {
-                e.printStackTrace();
+                Log.e(TAG, "HTTP Exception removing file " + mFileToRemove.getRemotePath(), e);
                 
             } catch (IOException e) {
-                e.printStackTrace();
+                Log.e(TAG, "I/O Exception removing file " + mFileToRemove.getRemotePath(), e);
                 
-            } finally {
-                if (!success) {
-                    mHandler.post(new Runnable() {
-                        @Override
-                        public void run() {
-                            try {
-                                Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); 
-                                msg.show();
-                            
-                            } catch (NotFoundException e) {
-                                e.printStackTrace();
+            } catch (Exception e) {
+                Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);
+            }
+            
+            if (success) {
+                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
+                fdsm.removeFile(mFileToRemove);
+                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();
+                        }
+                    }
+                });
             }
         }