Browse Source

Added failure handling to directory creation

David A. Velasco 13 years ago
parent
commit
76a123fc35

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.190B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.191B" 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" />

+ 4 - 0
res/values/strings.xml

@@ -159,6 +159,10 @@
 
     <string name="remove_success_msg">"Successful removal"</string>
     <string name="remove_fail_msg">"Removal 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>
 	
     <string name="filedisplay_unexpected_bad_get_content">"Unexpected problem ; please, try other app to select the file"</string>
 </resources>

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

@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.AlertDialog;
+import android.app.ProgressDialog;
 import android.app.AlertDialog.Builder;
 import android.app.Dialog;
 import android.content.BroadcastReceiver;
@@ -36,9 +37,11 @@ import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources.NotFoundException;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
 import android.support.v4.app.FragmentTransaction;
@@ -104,9 +107,13 @@ 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;
     
     private static final int ACTION_SELECT_FILE = 1;
     
+    private static final String TAG = "FileDisplayActivity";
+    
+    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i(getClass().toString(), "onCreate() start");
@@ -451,7 +458,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             } catch (NameNotFoundException e) {
                 builder = null;
                 dialog = null;
-                e.printStackTrace();
+                Log.e(TAG, "Error while showing about dialog", e);
             }
             break;
         }
@@ -486,18 +493,12 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                             
                             // Create directory
                             path += directoryName + OCFile.PATH_SEPARATOR;
-                            Thread thread = new Thread(new DirectoryCreator(path, a));
+                            Thread thread = new Thread(new DirectoryCreator(path, a, new Handler()));
                             thread.start();
-    
-                            // Save new directory in local database
-                            OCFile newDir = new OCFile(path);
-                            newDir.setMimetype("DIR");
-                            newDir.setParentId(mCurrentDir.getFileId());
-                            mStorageManager.saveFile(newDir);
-    
-                            // Display the new folder right away
+                            
                             dialog.dismiss();
-                            mFileList.listDirectory(mCurrentDir);
+                            
+                            showDialog(DIALOG_SHORT_WAIT);
                         }
                     });
             builder.setNegativeButton(R.string.common_cancel,
@@ -509,6 +510,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             dialog = builder.create();
             break;
         }
+        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;
         }
@@ -582,11 +592,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         private String mTargetPath;
         private Account mAccount;
         private AccountManager mAm;
+        private Handler mHandler; 
     
-        public DirectoryCreator(String targetPath, Account account) {
+        public DirectoryCreator(String targetPath, Account account, Handler handler) {
             mTargetPath = targetPath;
             mAccount = account;
             mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
+            mHandler = handler;
         }
     
         @Override
@@ -599,7 +611,39 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     
             wdc.setCredentials(username, password);
             wdc.allowSelfsignedCertificates();
-            wdc.createDirectory(mTargetPath);
+            boolean created = wdc.createDirectory(mTargetPath);
+            if (created) {
+                mHandler.post(new Runnable() {
+                    @Override
+                    public void run() { 
+                        dismissDialog(DIALOG_SHORT_WAIT);
+                        
+                        // Save new directory in local database
+                        OCFile newDir = new OCFile(mTargetPath);
+                        newDir.setMimetype("DIR");
+                        newDir.setParentId(mCurrentDir.getFileId());
+                        mStorageManager.saveFile(newDir);
+    
+                        // Display the new folder right away
+                        mFileList.listDirectory(mCurrentDir);
+                    }
+                });
+                
+            } else {
+                mHandler.post(new Runnable() {
+                    @Override
+                    public void run() {
+                        dismissDialog(DIALOG_SHORT_WAIT);
+                        try {
+                            Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG); 
+                            msg.show();
+                        
+                        } catch (NotFoundException e) {
+                            Log.e(TAG, "Error while trying to show fail message " , e);
+                        }
+                    }
+                });
+            }
         }
     
     }

+ 23 - 19
src/eu/alefzero/webdav/WebdavClient.java

@@ -142,7 +142,6 @@ public class WebdavClient extends HttpClient {
      */
     public boolean downloadFile(String remoteFilepath, File targetFile) {
         boolean ret = false;
-        boolean caughtException = false;
         GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));
 
         int status = -1;
@@ -166,19 +165,16 @@ public class WebdavClient extends HttpClient {
             
         } catch (HttpException e) {
             Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);
-            caughtException = true;
 
         } catch (IOException e) {
             Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);
-            caughtException = true;
 
         } catch (Exception e) {
             Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);
-            caughtException = true;
             
         } finally {
             if (!ret) {
-                if (!caughtException) {
+                if (status >= 0) {
                     Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);
                 }
                 if (targetFile.exists()) {
@@ -220,8 +216,7 @@ public class WebdavClient extends HttpClient {
      */
     public boolean putFile(String localFile, String remoteTarget, String contentType) {
         boolean result = false;
-        boolean caughtException = false;
-        int status = 0;
+        int status = -1;
 
         try {
             File f = new File(localFile);
@@ -237,18 +232,15 @@ public class WebdavClient extends HttpClient {
             
         } catch (HttpException e) {
             Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);
-            caughtException = true;
 
         } catch (IOException e) {
             Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);
-            caughtException = true;
 
         } catch (Exception e) {
             Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);
-            caughtException = true;
         }
         
-        if (!result && !caughtException) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);
+        if (!result && status >= 0) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);
         
         return result;
     }
@@ -284,17 +276,29 @@ public class WebdavClient extends HttpClient {
      * @return          'True' when the directory is successfully created
      */
     public boolean createDirectory(String path) {
+        boolean result = false;
+        int status = -1;
         try {
             MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));
-            int status = executeMethod(mkcol);
-            Log.d(TAG, "Status returned " + status);
-            Log.d(TAG, "uri: " + mkcol.getURI().toString());
-            Log.i(TAG, "Creating dir completed");
-        } catch (final Exception e) {
-            e.printStackTrace();
-            return false;
+            Log.d(TAG, "Creating directory " + path);
+            status = executeMethod(mkcol);
+            Log.d(TAG, "Status returned: " + status);
+            result = mkcol.succeeded();
+            
+        } catch (HttpException e) {
+            Log.e(TAG, "HTTP exception creating directory " + path, e);
+
+        } catch (IOException e) {
+            Log.e(TAG, "I/O exception creating directory " + path, e);
+
+        } catch (Exception e) {
+            Log.e(TAG, "Unexpected exception creating directory " + path, e);
+            
         }
-        return true;
+        if (!result && status >= 0) {
+            Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status);
+        }
+        return result;
     }