File removal in server completed
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 12 Jul 2012 11:49:21 +0000 (13:49 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 12 Jul 2012 11:49:21 +0000 (13:49 +0200)
AndroidManifest.xml
res/values/strings.xml
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

index db414b6..581d36b 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.155B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.156B" 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
index ca3eb89..b62ba9b 100644 (file)
     
     <string name="confirmation_alert_prefix">"Do you really want "</string>
     <string name="confirmation_alert_suffix">"?"</string>
+    
+    <string name="remove_success_msg">"Successful removal"</string>
+    <string name="remove_fail_msg">"Removal could not be completed"</string>
+    
 </resources>
index 381d4d8..efee656 100644 (file)
@@ -17,6 +17,7 @@
  */\r
 package eu.alefzero.owncloud.ui.fragment;\r
 \r
+import java.io.File;\r
 import java.io.IOException;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
@@ -35,6 +36,7 @@ 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.jackrabbit.webdav.client.methods.DeleteMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.MoveMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
 import org.json.JSONException;\r
@@ -51,6 +53,7 @@ import android.content.DialogInterface;
 import android.content.DialogInterface.OnDismissListener;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
+import android.content.res.Resources.NotFoundException;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
 import android.graphics.BitmapFactory.Options;\r
@@ -59,8 +62,10 @@ import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.os.Environment;\r
 import android.os.Handler;\r
 import android.preference.PreferenceActivity.Header;\r
+import android.support.v4.app.FragmentTransaction;\r
 import android.util.Log;\r
 import android.view.Display;\r
 import android.view.LayoutInflater;\r
@@ -86,6 +91,7 @@ import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.owncloud.files.services.FileDownloader;\r
+import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
 import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
 import eu.alefzero.webdav.WebdavClient;\r
 \r
@@ -110,6 +116,7 @@ public class FileDetailFragment extends SherlockFragment implements
 \r
     private static final String TAG = "FileDetailFragment";\r
     public static final String FTAG = "FileDetails"; \r
+    public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";\r
 \r
     \r
     /**\r
@@ -158,10 +165,10 @@ public class FileDetailFragment extends SherlockFragment implements
         mView = view;\r
         \r
         if (mLayout == R.layout.file_details_fragment) {\r
-            getView().findViewById(R.id.fdKeepInSync).setOnClickListener(this);\r
-            //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
-            getView().findViewById(R.id.fdRenameBtn).setOnClickListener(this);\r
-            getView().findViewById(R.id.fdRemoveBtn).setOnClickListener(this);\r
+            mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);\r
+            //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
+            mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this);\r
+            mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);\r
         }\r
         \r
         updateFileDetails();\r
@@ -235,7 +242,7 @@ public class FileDetailFragment extends SherlockFragment implements
             case R.id.fdRemoveBtn: {\r
                 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance("to remove " + mFile.getFileName());\r
                 confDialog.setOnConfirmationListener(this);\r
-                confDialog.show(getFragmentManager(), "REMOVE_CONFIRMATION_FRAGMENT");\r
+                confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);\r
                 break;\r
             }\r
             default:\r
@@ -251,9 +258,13 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     @Override\r
     public void onConfirmation(boolean confirmation, String callerTag) {\r
-        if (confirmation && callerTag.equals("REMOVE_CONFIRMATION_FRAGMENT")) {\r
-            // TODO remove in a separated thread\r
-        }\r
+        if (confirmation && callerTag.equals(FTAG_CONFIRMATION)) {\r
+            Log.e("ASD","onConfirmation");\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
+            }\r
+        } else if (!confirmation) Log.d(TAG, "REMOVAL CANCELED");\r
     }\r
     \r
     \r
@@ -794,5 +805,90 @@ public class FileDetailFragment extends SherlockFragment implements
         }\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 = new WebdavClient(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 + mFileToRemove.getRemotePath());\r
+\r
+            DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + mFileToRemove.getRemotePath());\r
+            HttpMethodParams params = delete.getParams();\r
+            params.setSoTimeout(1000);\r
+            delete.setParams(params);\r
+            \r
+            boolean success = false;\r
+            try {\r
+                int status = wc.executeMethod(delete);\r
+                if (delete.succeeded()) {\r
+                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
+                    fdsm.removeFile(mFileToRemove);\r
+                    mHandler.post(new Runnable() {\r
+                        @Override\r
+                        public void run() { \r
+                            try {\r
+                                Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);\r
+                                msg.show();\r
+                                if (getActivity() instanceof FileDisplayActivity) {\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
+                                    \r
+                                } else {\r
+                                    getActivity().finish();\r
+                                }\r
+                                \r
+                            } catch (NotFoundException e) {\r
+                                e.printStackTrace();\r
+                            }\r
+                        }\r
+                    });\r
+                    success = true;\r
+                }\r
+                Log.e("ASD", ""+ delete.getQueryString());\r
+                Log.d("delete", "returned status " + status);\r
+                \r
+            } catch (HttpException e) {\r
+                e.printStackTrace();\r
+                \r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
+                \r
+            } finally {\r
+                if (!success) {\r
+                    mHandler.post(new Runnable() {\r
+                        @Override\r
+                        public void run() {\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
+    \r
 \r
 }\r