Being careful with regressions
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index 9b41093..c94719e 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
@@ -42,6 +44,8 @@ import org.json.JSONObject;
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
+import android.app.AlertDialog;\r
+import android.app.Dialog;\r
 import android.content.ActivityNotFoundException;\r
 import android.content.BroadcastReceiver;\r
 import android.content.Context;\r
@@ -49,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
@@ -56,9 +61,12 @@ import android.graphics.Point;
 import android.graphics.drawable.BitmapDrawable;\r
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
+import android.os.AsyncTask;\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
@@ -84,6 +92,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
@@ -94,7 +103,7 @@ import eu.alefzero.webdav.WebdavClient;
  * \r
  */\r
 public class FileDetailFragment extends SherlockFragment implements\r
-        OnClickListener {\r
+        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener {\r
 \r
     public static final String EXTRA_FILE = "FILE";\r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
@@ -103,11 +112,13 @@ public class FileDetailFragment extends SherlockFragment implements
     private View mView;\r
     private OCFile mFile;\r
     private Account mAccount;\r
+    private ImageView mPreview;\r
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
 \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
@@ -155,6 +166,14 @@ public class FileDetailFragment extends SherlockFragment implements
         view = inflater.inflate(mLayout, container, false);\r
         mView = view;\r
         \r
+        if (mLayout == R.layout.file_details_fragment) {\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
+            mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
+        }\r
+        \r
         updateFileDetails();\r
         return view;\r
     }\r
@@ -184,6 +203,9 @@ public class FileDetailFragment extends SherlockFragment implements
         super.onPause();\r
         getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
         mDownloadFinishReceiver = null;\r
+        if (mPreview != null) {\r
+            mPreview = null;\r
+        }\r
     }\r
 \r
     @Override\r
@@ -222,6 +244,12 @@ public class FileDetailFragment extends SherlockFragment implements
                 dialog.show(getFragmentManager(), "nameeditdialog");\r
                 dialog.setOnDismissListener(this);\r
                 break;\r
+            }   \r
+            case R.id.fdRemoveBtn: {\r
+                ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance("to remove " + mFile.getFileName());\r
+                confDialog.setOnConfirmationListener(this);\r
+                confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);\r
+                break;\r
             }\r
             default:\r
                 Log.e(TAG, "Incorrect view clicked!");\r
@@ -232,8 +260,20 @@ public class FileDetailFragment extends SherlockFragment implements
             t.start();\r
         }*/\r
     }\r
-\r
-\r
+    \r
+    \r
+    @Override\r
+    public void onConfirmation(boolean confirmation, String callerTag) {\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
     /**\r
      * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.\r
      * \r
@@ -285,75 +325,12 @@ public class FileDetailFragment extends SherlockFragment implements
             \r
             CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);\r
             cb.setChecked(mFile.keepInSync());\r
-            cb.setOnClickListener(this);\r
-            //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
-            getView().findViewById(R.id.fdRenameBtn).setOnClickListener(this);\r
             \r
             if (mFile.getStoragePath() != null) {\r
                 // Update preview\r
-                ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
-                try {\r
-                    if (mFile.getMimetype().startsWith("image/")) {\r
-                        BitmapFactory.Options options = new Options();\r
-                        options.inScaled = true;\r
-                        options.inPurgeable = true;\r
-                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {\r
-                            options.inPreferQualityOverSpeed = false;\r
-                        }\r
-                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {\r
-                            options.inMutable = false;\r
-                        }\r
-\r
-                        Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
-\r
-                        if (bmp != null) {\r
-                            int width = options.outWidth;\r
-                            int height = options.outHeight;\r
-                            int scale = 1;\r
-                            boolean recycle = false;\r
-                            if (width >= 2048 || height >= 2048) {\r
-                                scale = (int) (Math.ceil(Math.max(height, width)/2048.));\r
-                                options.inSampleSize = scale;\r
-                                recycle = true;\r
-                            }\r
-                                Display display = getActivity().getWindowManager().getDefaultDisplay();\r
-                                Point size = new Point();\r
-                                int screenwidth;\r
-                                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
-                                    display.getSize(size);\r
-                                    screenwidth = size.x;\r
-                                } else {\r
-                                    screenwidth = display.getWidth();\r
-                                }\r
-\r
-                                Log.e("ASD", "W " + width + " SW " + screenwidth);\r
-\r
-                                if (width > screenwidth) {\r
-                                    scale = (int) (Math.ceil(Math.max(height, width)/screenwidth));\r
-                                    options.inSampleSize = scale;\r
-                                    recycle = true;\r
-                                }\r
-                            \r
-\r
-                            if (recycle) bmp.recycle();\r
-                            bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
-                            \r
-                        }\r
-                        if (bmp != null) {\r
-                            preview.setImageBitmap(bmp);\r
-                        }\r
-                    }\r
-                } catch (OutOfMemoryError e) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
-                    \r
-                } catch (NoSuchFieldError e) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Error from access to unexisting field despite protection " + mFile.getFileLength());\r
-                    \r
-                } catch (Throwable t) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);\r
+                if (mFile.getMimetype().startsWith("image/")) {\r
+                    BitmapLoader bl = new BitmapLoader();\r
+                    bl.execute(new String[]{mFile.getStoragePath()});\r
                 }\r
                 \r
                 // Change download button to open button\r
@@ -502,7 +479,7 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
     }\r
     \r
-    // this is a temporary class for sharing purposes, it need to be replacead in transfer service\r
+    // this is a temporary class for sharing purposes, it need to be replaced in transfer service\r
     private class ShareRunnable implements Runnable {\r
         private String mPath;\r
 \r
@@ -732,7 +709,9 @@ public class FileDetailFragment extends SherlockFragment implements
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\r
             View v = inflater.inflate(R.layout.edit_box_dialog, container, false);\r
 \r
-            String currentName = getArguments().getString("filename", "");\r
+            String currentName = getArguments().getString("filename");\r
+            if (currentName == null)\r
+                currentName = "";\r
             \r
             ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this);\r
             ((Button)v.findViewById(R.id.ok)).setOnClickListener(this);\r
@@ -772,4 +751,164 @@ 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
+    class BitmapLoader extends AsyncTask<String, Void, Bitmap> {\r
+        @Override\r
+        protected Bitmap doInBackground(String... params) {\r
+            Bitmap result = null;\r
+            if (params.length != 1) return result;\r
+            String storagePath = params[0];\r
+            try {\r
+\r
+                BitmapFactory.Options options = new Options();\r
+                options.inScaled = true;\r
+                options.inPurgeable = true;\r
+                options.inJustDecodeBounds = true;\r
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {\r
+                    options.inPreferQualityOverSpeed = false;\r
+                }\r
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {\r
+                    options.inMutable = false;\r
+                }\r
+\r
+                result = BitmapFactory.decodeFile(storagePath, options);\r
+                options.inJustDecodeBounds = false;\r
+\r
+                int width = options.outWidth;\r
+                int height = options.outHeight;\r
+                int scale = 1;\r
+                boolean recycle = false;\r
+                if (width >= 2048 || height >= 2048) {\r
+                    scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));\r
+                    options.inSampleSize = scale;\r
+                }\r
+                Display display = getActivity().getWindowManager().getDefaultDisplay();\r
+                Point size = new Point();\r
+                int screenwidth;\r
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
+                    display.getSize(size);\r
+                    screenwidth = size.x;\r
+                } else {\r
+                    screenwidth = display.getWidth();\r
+                }\r
+\r
+                Log.e("ASD", "W " + width + " SW " + screenwidth);\r
+\r
+                if (width > screenwidth) {\r
+                    scale = (int) Math.ceil((float)width / screenwidth);\r
+                    options.inSampleSize = scale;\r
+                }\r
+\r
+                result = BitmapFactory.decodeFile(storagePath, options);\r
+\r
+                Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight);\r
+\r
+            } catch (OutOfMemoryError e) {\r
+                result = null;\r
+                Log.e(TAG, "Out of memory occured for file with size " + storagePath);\r
+                \r
+            } catch (NoSuchFieldError e) {\r
+                result = null;\r
+                Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);\r
+                \r
+            } catch (Throwable t) {\r
+                result = null;\r
+                Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);\r
+            }\r
+            return result;\r
+        }\r
+        @Override\r
+        protected void onPostExecute(Bitmap result) {\r
+            if (result != null) {\r
+                mPreview.setImageBitmap(result);\r
+            }\r
+        }\r
+        \r
+    }\r
+    \r
+\r
 }\r