More fixes to PIN Code activity
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index efee656..34a82cb 100644 (file)
@@ -61,6 +61,7 @@ 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
@@ -94,6 +95,7 @@ import eu.alefzero.owncloud.files.services.FileDownloader;
 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
 import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
 import eu.alefzero.webdav.WebdavClient;\r
+import eu.alefzero.webdav.WebdavUtils;\r
 \r
 /**\r
  * This Fragment is used to display the details about a file.\r
@@ -111,6 +113,7 @@ 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
@@ -169,6 +172,7 @@ public class FileDetailFragment extends SherlockFragment implements
             //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
@@ -193,6 +197,7 @@ public class FileDetailFragment extends SherlockFragment implements
         IntentFilter filter = new IntentFilter(\r
                 FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
         getActivity().registerReceiver(mDownloadFinishReceiver, filter);\r
+        mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
     }\r
 \r
     @Override\r
@@ -200,6 +205,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
@@ -217,7 +225,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 Intent i = new Intent(getActivity(), FileDownloader.class);\r
                 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
                 i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
-                i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getURLDecodedRemotePath());\r
+                i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());\r
                 i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
                 v.setEnabled(false);\r
                 getActivity().startService(i);\r
@@ -228,7 +236,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 mFile.setKeepInSync(cb.isChecked());\r
                 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
                 fdsm.saveFile(mFile);\r
-                if (mFile.keepInSync() && !mFile.isDownloaded()) {\r
+                if (mFile.keepInSync()) {\r
                     onClick(getView().findViewById(R.id.fdDownloadBtn));\r
                 }\r
                 break;\r
@@ -322,69 +330,9 @@ public class FileDetailFragment extends SherlockFragment implements
             \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
@@ -393,9 +341,10 @@ public class FileDetailFragment extends SherlockFragment implements
                     @Override\r
                     public void onClick(View v) {\r
                         String storagePath = mFile.getStoragePath();\r
+                        String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
                         try {\r
                             Intent i = new Intent(Intent.ACTION_VIEW);\r
-                            i.setDataAndType(Uri.parse("file://"+ storagePath), mFile.getMimetype());\r
+                            i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());\r
                             i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
                             startActivity(i);\r
                             \r
@@ -407,7 +356,7 @@ public class FileDetailFragment extends SherlockFragment implements
                                 Intent i = new Intent(Intent.ACTION_VIEW);\r
                                 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
                                 if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {\r
-                                    i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mimeType);\r
+                                    i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
                                     i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
                                     startActivity(i);\r
                                     toastIt = false;\r
@@ -648,7 +597,6 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
     public void onDismiss(EditNameFragment dialog) {\r
-        Log.e("ASD","ondismiss");\r
         if (dialog instanceof EditNameFragment) {\r
             if (((EditNameFragment)dialog).getResult()) {\r
                 String newFilename = ((EditNameFragment)dialog).getNewFilename();\r
@@ -656,7 +604,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 if (!newFilename.equals(mFile.getFileName())) {\r
                     FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
                     if (fdsm.getFileById(mFile.getFileId()) != null) {\r
-                        OCFile newFile = new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath()+"/"+newFilename);\r
+                        OCFile newFile = new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + OCFile.PATH_SEPARATOR + newFilename);\r
                         newFile.setCreationTimestamp(mFile.getCreationTimestamp());\r
                         newFile.setFileId(mFile.getFileId());\r
                         newFile.setFileLength(mFile.getFileLength());\r
@@ -665,7 +613,13 @@ public class FileDetailFragment extends SherlockFragment implements
                         newFile.setMimetype(mFile.getMimetype());\r
                         newFile.setModificationTimestamp(mFile.getModificationTimestamp());\r
                         newFile.setParentId(mFile.getParentId());\r
-                        newFile.setStoragePath(mFile.getStoragePath());\r
+                        if (mFile.isDownloaded()) {\r
+                            File f = new File(mFile.getStoragePath());\r
+                            Log.e(TAG, f.getAbsolutePath()+"");\r
+                            f.renameTo(new File(f.getParent()+"/"+newFilename));\r
+                            Log.e(TAG, f.getAbsolutePath()+"");\r
+                            newFile.setStoragePath(f.getAbsolutePath());\r
+                        }\r
                         \r
                         new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();\r
 \r
@@ -697,11 +651,11 @@ public class FileDetailFragment extends SherlockFragment implements
             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 + mOld.getRemotePath());\r
+            Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()));\r
 \r
-            Log.e("ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + mNew.getRemotePath());\r
-            LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + mOld.getRemotePath(),\r
-                                             Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + mNew.getRemotePath());\r
+            Log.e("ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));\r
+            LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),\r
+                                             Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));\r
             \r
             try {\r
                 int status = wc.executeMethod(move);\r
@@ -806,9 +760,11 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
     }\r
     \r
-    \r
     private class RemoveRunnable implements Runnable {\r
         \r
+        /** Arbitrary timeout for deletion */\r
+        public final static int DELETION_TIMEOUT = 5000;\r
+        \r
         Account mAccount;\r
         OCFile mFileToRemove;\r
         Handler mHandler;\r
@@ -825,16 +781,13 @@ public class FileDetailFragment extends SherlockFragment implements
             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
+            Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(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
+            DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
             \r
             boolean success = false;\r
             try {\r
-                int status = wc.executeMethod(delete);\r
+                int status = wc.executeMethod(delete, DELETION_TIMEOUT);\r
                 if (delete.succeeded()) {\r
                     FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
                     fdsm.removeFile(mFileToRemove);\r
@@ -890,5 +843,79 @@ public class FileDetailFragment extends SherlockFragment implements
         \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 && mPreview != null) {\r
+                mPreview.setImageBitmap(result);\r
+            }\r
+        }\r
+        \r
+    }\r
+    \r
 \r
 }\r