Added failure handling to directory creation
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 30 Jul 2012 12:06:53 +0000 (14:06 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 30 Jul 2012 12:06:53 +0000 (14:06 +0200)
AndroidManifest.xml
res/values/strings.xml
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java
src/eu/alefzero/webdav/WebdavClient.java

index 4c27b14..2e1a9fa 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.190B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.191B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 566a73d..cf8adf5 100644 (file)
 
     <string name="remove_success_msg">"Successful removal"</string>
     <string name="remove_fail_msg">"Removal could not be completed"</string>
 
     <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>
        
     <string name="filedisplay_unexpected_bad_get_content">"Unexpected problem ; please, try other app to select the file"</string>
 </resources>
index ce89b87..b4365d0 100644 (file)
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.AlertDialog;\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.app.AlertDialog;\r
+import android.app.ProgressDialog;\r
 import android.app.AlertDialog.Builder;\r
 import android.app.Dialog;\r
 import android.content.BroadcastReceiver;\r
 import android.app.AlertDialog.Builder;\r
 import android.app.Dialog;\r
 import android.content.BroadcastReceiver;\r
@@ -36,9 +37,11 @@ import android.content.IntentFilter;
 import android.content.SharedPreferences;\r
 import android.content.pm.PackageInfo;\r
 import android.content.pm.PackageManager.NameNotFoundException;\r
 import android.content.SharedPreferences;\r
 import android.content.pm.PackageInfo;\r
 import android.content.pm.PackageManager.NameNotFoundException;\r
+import android.content.res.Resources.NotFoundException;\r
 import android.database.Cursor;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.database.Cursor;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.os.Handler;\r
 import android.preference.PreferenceManager;\r
 import android.provider.MediaStore;\r
 import android.support.v4.app.FragmentTransaction;\r
 import android.preference.PreferenceManager;\r
 import android.provider.MediaStore;\r
 import android.support.v4.app.FragmentTransaction;\r
@@ -104,9 +107,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private static final int DIALOG_SETUP_ACCOUNT = 0;\r
     private static final int DIALOG_CREATE_DIR = 1;\r
     private static final int DIALOG_ABOUT_APP = 2;\r
     private static final int DIALOG_SETUP_ACCOUNT = 0;\r
     private static final int DIALOG_CREATE_DIR = 1;\r
     private static final int DIALOG_ABOUT_APP = 2;\r
+    private static final int DIALOG_SHORT_WAIT = 3;\r
     \r
     private static final int ACTION_SELECT_FILE = 1;\r
     \r
     \r
     private static final int ACTION_SELECT_FILE = 1;\r
     \r
+    private static final String TAG = "FileDisplayActivity";\r
+    \r
+    \r
     @Override\r
     public void onCreate(Bundle savedInstanceState) {\r
         Log.i(getClass().toString(), "onCreate() start");\r
     @Override\r
     public void onCreate(Bundle savedInstanceState) {\r
         Log.i(getClass().toString(), "onCreate() start");\r
@@ -451,7 +458,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             } catch (NameNotFoundException e) {\r
                 builder = null;\r
                 dialog = null;\r
             } catch (NameNotFoundException e) {\r
                 builder = null;\r
                 dialog = null;\r
-                e.printStackTrace();\r
+                Log.e(TAG, "Error while showing about dialog", e);\r
             }\r
             break;\r
         }\r
             }\r
             break;\r
         }\r
@@ -486,18 +493,12 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                             \r
                             // Create directory\r
                             path += directoryName + OCFile.PATH_SEPARATOR;\r
                             \r
                             // Create directory\r
                             path += directoryName + OCFile.PATH_SEPARATOR;\r
-                            Thread thread = new Thread(new DirectoryCreator(path, a));\r
+                            Thread thread = new Thread(new DirectoryCreator(path, a, new Handler()));\r
                             thread.start();\r
                             thread.start();\r
-    \r
-                            // Save new directory in local database\r
-                            OCFile newDir = new OCFile(path);\r
-                            newDir.setMimetype("DIR");\r
-                            newDir.setParentId(mCurrentDir.getFileId());\r
-                            mStorageManager.saveFile(newDir);\r
-    \r
-                            // Display the new folder right away\r
+                            \r
                             dialog.dismiss();\r
                             dialog.dismiss();\r
-                            mFileList.listDirectory(mCurrentDir);\r
+                            \r
+                            showDialog(DIALOG_SHORT_WAIT);\r
                         }\r
                     });\r
             builder.setNegativeButton(R.string.common_cancel,\r
                         }\r
                     });\r
             builder.setNegativeButton(R.string.common_cancel,\r
@@ -509,6 +510,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             dialog = builder.create();\r
             break;\r
         }\r
             dialog = builder.create();\r
             break;\r
         }\r
+        case DIALOG_SHORT_WAIT: {\r
+            ProgressDialog working_dialog = new ProgressDialog(this);\r
+            working_dialog.setMessage(getResources().getString(\r
+                    R.string.wait_a_moment));\r
+            working_dialog.setIndeterminate(true);\r
+            working_dialog.setCancelable(false);\r
+            dialog = working_dialog;\r
+            break;\r
+        }\r
         default:\r
             dialog = null;\r
         }\r
         default:\r
             dialog = null;\r
         }\r
@@ -582,11 +592,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         private String mTargetPath;\r
         private Account mAccount;\r
         private AccountManager mAm;\r
         private String mTargetPath;\r
         private Account mAccount;\r
         private AccountManager mAm;\r
+        private Handler mHandler; \r
     \r
     \r
-        public DirectoryCreator(String targetPath, Account account) {\r
+        public DirectoryCreator(String targetPath, Account account, Handler handler) {\r
             mTargetPath = targetPath;\r
             mAccount = account;\r
             mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
             mTargetPath = targetPath;\r
             mAccount = account;\r
             mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
+            mHandler = handler;\r
         }\r
     \r
         @Override\r
         }\r
     \r
         @Override\r
@@ -599,7 +611,39 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     \r
             wdc.setCredentials(username, password);\r
             wdc.allowSelfsignedCertificates();\r
     \r
             wdc.setCredentials(username, password);\r
             wdc.allowSelfsignedCertificates();\r
-            wdc.createDirectory(mTargetPath);\r
+            boolean created = wdc.createDirectory(mTargetPath);\r
+            if (created) {\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() { \r
+                        dismissDialog(DIALOG_SHORT_WAIT);\r
+                        \r
+                        // Save new directory in local database\r
+                        OCFile newDir = new OCFile(mTargetPath);\r
+                        newDir.setMimetype("DIR");\r
+                        newDir.setParentId(mCurrentDir.getFileId());\r
+                        mStorageManager.saveFile(newDir);\r
+    \r
+                        // Display the new folder right away\r
+                        mFileList.listDirectory(mCurrentDir);\r
+                    }\r
+                });\r
+                \r
+            } else {\r
+                mHandler.post(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        dismissDialog(DIALOG_SHORT_WAIT);\r
+                        try {\r
+                            Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG); \r
+                            msg.show();\r
+                        \r
+                        } catch (NotFoundException e) {\r
+                            Log.e(TAG, "Error while trying to show fail message " , e);\r
+                        }\r
+                    }\r
+                });\r
+            }\r
         }\r
     \r
     }\r
         }\r
     \r
     }\r
index 0f894bf..1135daf 100644 (file)
@@ -142,7 +142,6 @@ public class WebdavClient extends HttpClient {
      */\r
     public boolean downloadFile(String remoteFilepath, File targetFile) {\r
         boolean ret = false;\r
      */\r
     public boolean downloadFile(String remoteFilepath, File targetFile) {\r
         boolean ret = false;\r
-        boolean caughtException = false;\r
         GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
 \r
         int status = -1;\r
         GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
 \r
         int status = -1;\r
@@ -166,19 +165,16 @@ public class WebdavClient extends HttpClient {
             \r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);\r
             \r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
 \r
         } catch (IOException e) {\r
             Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);\r
 \r
         } catch (IOException e) {\r
             Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
 \r
         } catch (Exception e) {\r
             Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);\r
 \r
         } catch (Exception e) {\r
             Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
             \r
         } finally {\r
             if (!ret) {\r
             \r
         } finally {\r
             if (!ret) {\r
-                if (!caughtException) {\r
+                if (status >= 0) {\r
                     Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);\r
                 }\r
                 if (targetFile.exists()) {\r
                     Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);\r
                 }\r
                 if (targetFile.exists()) {\r
@@ -220,8 +216,7 @@ public class WebdavClient extends HttpClient {
      */\r
     public boolean putFile(String localFile, String remoteTarget, String contentType) {\r
         boolean result = false;\r
      */\r
     public boolean putFile(String localFile, String remoteTarget, String contentType) {\r
         boolean result = false;\r
-        boolean caughtException = false;\r
-        int status = 0;\r
+        int status = -1;\r
 \r
         try {\r
             File f = new File(localFile);\r
 \r
         try {\r
             File f = new File(localFile);\r
@@ -237,18 +232,15 @@ public class WebdavClient extends HttpClient {
             \r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);\r
             \r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);\r
-            caughtException = true;\r
 \r
         } catch (IOException e) {\r
             Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);\r
 \r
         } catch (IOException e) {\r
             Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);\r
-            caughtException = true;\r
 \r
         } catch (Exception e) {\r
             Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);\r
 \r
         } catch (Exception e) {\r
             Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);\r
-            caughtException = true;\r
         }\r
         \r
         }\r
         \r
-        if (!result && !caughtException) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);\r
+        if (!result && status >= 0) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);\r
         \r
         return result;\r
     }\r
         \r
         return result;\r
     }\r
@@ -284,17 +276,29 @@ public class WebdavClient extends HttpClient {
      * @return          'True' when the directory is successfully created\r
      */\r
     public boolean createDirectory(String path) {\r
      * @return          'True' when the directory is successfully created\r
      */\r
     public boolean createDirectory(String path) {\r
+        boolean result = false;\r
+        int status = -1;\r
         try {\r
             MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
         try {\r
             MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
-            int status = executeMethod(mkcol);\r
-            Log.d(TAG, "Status returned " + status);\r
-            Log.d(TAG, "uri: " + mkcol.getURI().toString());\r
-            Log.i(TAG, "Creating dir completed");\r
-        } catch (final Exception e) {\r
-            e.printStackTrace();\r
-            return false;\r
+            Log.d(TAG, "Creating directory " + path);\r
+            status = executeMethod(mkcol);\r
+            Log.d(TAG, "Status returned: " + status);\r
+            result = mkcol.succeeded();\r
+            \r
+        } catch (HttpException e) {\r
+            Log.e(TAG, "HTTP exception creating directory " + path, e);\r
+\r
+        } catch (IOException e) {\r
+            Log.e(TAG, "I/O exception creating directory " + path, e);\r
+\r
+        } catch (Exception e) {\r
+            Log.e(TAG, "Unexpected exception creating directory " + path, e);\r
+            \r
         }\r
         }\r
-        return true;\r
+        if (!result && status >= 0) {\r
+            Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status);\r
+        }\r
+        return result;\r
     }\r
     \r
     \r
     }\r
     \r
     \r