fix download file with noascii chars in name
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index e887876..f76c2b2 100644 (file)
@@ -23,7 +23,9 @@ import android.content.Intent;
 import android.content.IntentFilter;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
+import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.util.Log;\r
 import android.view.LayoutInflater;\r
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
@@ -56,6 +58,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private int mLayout;\r
     private View mView;\r
     private OCFile mFile;\r
+    private static final String TAG = "FileDetailFragment";\r
 \r
     /**\r
      * Default constructor - contains real layout\r
@@ -78,6 +81,18 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
     \r
+    /**\r
+     * Use this when creating the fragment and display\r
+     * a file at the same time\r
+     * \r
+     * @param showDetailsIntent The Intent with the required parameters\r
+     * @see FileDetailFragment#updateFileDetails(Intent)\r
+     */\r
+    public FileDetailFragment(Intent showDetailsIntent) {\r
+        mIntent = showDetailsIntent;\r
+        mLayout = R.layout.file_details_fragment;\r
+    }\r
+\r
     @Override\r
     public void onResume() {\r
         super.onResume();\r
@@ -112,6 +127,7 @@ public class FileDetailFragment extends SherlockFragment implements
 \r
     private void updateFileDetails() {\r
         mFile = mIntent.getParcelableExtra(FILE);\r
+        Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
 \r
         if (mFile != null) {\r
             // set file details\r
@@ -119,20 +135,34 @@ public class FileDetailFragment extends SherlockFragment implements
             setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
                     .getMimetype()));\r
             setFilesize(mFile.getFileLength());\r
+            setTimeCreated(mFile.getCreationTimestamp());\r
+            setTimeModified(mFile.getModificationTimestamp());\r
             \r
             // Update preview\r
             if (mFile.getStoragePath() != null) {\r
-                if (mFile.getMimetype().startsWith("image/")) {\r
-                    ImageView preview = (ImageView) getView().findViewById(\r
-                            R.id.fdPreview);\r
-                    Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
-                    preview.setImageBitmap(bmp);\r
+                try {\r
+                    if (mFile.getMimetype().startsWith("image/")) {\r
+                        ImageView preview = (ImageView) getView().findViewById(\r
+                                R.id.fdPreview);\r
+                        Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
+                        preview.setImageBitmap(bmp);\r
+                    }\r
+                } catch (OutOfMemoryError e) {\r
+                    Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
                 }\r
+                downloadButton.setText("Open file");\r
+                downloadButton.setOnClickListener(new OnClickListener() {\r
+                    @Override\r
+                    public void onClick(View v) {\r
+                        Intent i = new Intent(Intent.ACTION_VIEW);\r
+                        i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());\r
+                        startActivity(i);\r
+                    }\r
+                });\r
+            } else {\r
+                // Make download button effective\r
+                downloadButton.setOnClickListener(this);\r
             }\r
-            \r
-            // Make download button effective\r
-            Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setOnClickListener(this);\r
         }\r
     }\r
     \r
@@ -151,7 +181,21 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setFilesize(long filesize) {\r
         TextView tv = (TextView) getView().findViewById(R.id.fdSize);\r
         if (tv != null)\r
-            tv.setText(DisplayUtils.bitsToHumanReadable(filesize));\r
+            tv.setText(DisplayUtils.bytesToHumanReadable(filesize));\r
+    }\r
+    \r
+    private void setTimeCreated(long milliseconds){\r
+        TextView tv = (TextView) getView().findViewById(R.id.fdCreated);\r
+        if(tv != null){\r
+            tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));\r
+        }\r
+    }\r
+    \r
+    private void setTimeModified(long milliseconds){\r
+        TextView tv = (TextView) getView().findViewById(R.id.fdModified);\r
+        if(tv != null){\r
+            tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));\r
+        }\r
     }\r
 \r
     @Override\r
@@ -161,7 +205,10 @@ public class FileDetailFragment extends SherlockFragment implements
         view = inflater.inflate(mLayout, container, false);\r
         mView = view;\r
         if(mLayout == R.layout.file_details_fragment){\r
-            mIntent = getActivity().getIntent();\r
+            // Phones will launch an activity with this intent\r
+            if(mIntent == null){\r
+                mIntent = getActivity().getIntent();\r
+            }\r
             updateFileDetails();\r
         }\r
         \r