Display modified / created timestamp, JavaDocs in DisplayUtils
authorLennart Rosam <lennart@familie-rosam.de>
Tue, 22 May 2012 16:17:16 +0000 (18:17 +0200)
committerLennart Rosam <lennart@familie-rosam.de>
Tue, 22 May 2012 16:17:16 +0000 (18:17 +0200)
src/eu/alefzero/owncloud/DisplayUtils.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

index 08afd65..bf86c6f 100644 (file)
@@ -18,6 +18,7 @@
 \r
 package eu.alefzero.owncloud;\r
 \r
 \r
 package eu.alefzero.owncloud;\r
 \r
+import java.util.Date;\r
 import java.util.HashMap;\r
 \r
 /**\r
 import java.util.HashMap;\r
 \r
 /**\r
@@ -27,8 +28,34 @@ import java.util.HashMap;
  * \r
  */\r
 public class DisplayUtils {\r
  * \r
  */\r
 public class DisplayUtils {\r
-    public static String bitsToHumanReadable(long bitsLen) {\r
-        double result = bitsLen;\r
+\r
+    private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
+\r
+    private static HashMap<String, String> mimeType2HUmanReadable;\r
+    static {\r
+        mimeType2HUmanReadable = new HashMap<String, String>();\r
+        // images\r
+        mimeType2HUmanReadable.put("image/jpeg", "JPEG image");\r
+        mimeType2HUmanReadable.put("image/jpg", "JPEG image");\r
+        mimeType2HUmanReadable.put("image/png", "PNG image");\r
+        mimeType2HUmanReadable.put("image/bmp", "Bitmap image");\r
+        mimeType2HUmanReadable.put("image/gif", "GIF image");\r
+        mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");\r
+        mimeType2HUmanReadable.put("image/tiff", "TIFF image");\r
+        // music\r
+        mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");\r
+        mimeType2HUmanReadable.put("application/ogg", "OGG music file");\r
+\r
+    }\r
+\r
+    /**\r
+     * Converts the file size in bytes to human readable output.\r
+     * \r
+     * @param bytes Input file size\r
+     * @return Like something readable like "12 MB"\r
+     */\r
+    public static String bytesToHumanReadable(long bytes) {\r
+        double result = bytes;\r
         int attachedsuff = 0;\r
         while (result > 1024 && attachedsuff < suffixes.length) {\r
             result /= 1024.;\r
         int attachedsuff = 0;\r
         while (result > 1024 && attachedsuff < suffixes.length) {\r
             result /= 1024.;\r
@@ -38,7 +65,18 @@ public class DisplayUtils {
         return result + " " + suffixes[attachedsuff];\r
     }\r
 \r
         return result + " " + suffixes[attachedsuff];\r
     }\r
 \r
+    /**\r
+     * Removes special HTML entities from a string\r
+     * \r
+     * @param s Input string\r
+     * @return A cleaned version of the string\r
+     */\r
     public static String HtmlDecode(String s) {\r
     public static String HtmlDecode(String s) {\r
+        /*\r
+         * TODO: Perhaps we should use something more proven like:\r
+         * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29\r
+         */\r
+\r
         String ret = "";\r
         for (int i = 0; i < s.length(); ++i) {\r
             if (s.charAt(i) == '%') {\r
         String ret = "";\r
         for (int i = 0; i < s.length(); ++i) {\r
             if (s.charAt(i) == '%') {\r
@@ -51,6 +89,13 @@ public class DisplayUtils {
         return ret;\r
     }\r
 \r
         return ret;\r
     }\r
 \r
+    /**\r
+     * Converts MIME types like "image/jpg" to more end user friendly output\r
+     * like "JPG image".\r
+     * \r
+     * @param mimetype MIME type to convert\r
+     * @return A human friendly version of the MIME type\r
+     */\r
     public static String convertMIMEtoPrettyPrint(String mimetype) {\r
         if (mimeType2HUmanReadable.containsKey(mimetype)) {\r
             return mimeType2HUmanReadable.get(mimetype);\r
     public static String convertMIMEtoPrettyPrint(String mimetype) {\r
         if (mimeType2HUmanReadable.containsKey(mimetype)) {\r
             return mimeType2HUmanReadable.get(mimetype);\r
@@ -58,23 +103,13 @@ public class DisplayUtils {
         return mimetype.split("/")[1].toUpperCase() + " file";\r
     }\r
 \r
         return mimetype.split("/")[1].toUpperCase() + " file";\r
     }\r
 \r
-    private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB",\r
-            "PB", "EB", "ZB", "YB" };\r
-\r
-    private static HashMap<String, String> mimeType2HUmanReadable;\r
-    static {\r
-        mimeType2HUmanReadable = new HashMap<String, String>();\r
-        // images\r
-        mimeType2HUmanReadable.put("image/jpeg", "JPEG image");\r
-        mimeType2HUmanReadable.put("image/jpg", "JPEG image");\r
-        mimeType2HUmanReadable.put("image/png", "PNG image");\r
-        mimeType2HUmanReadable.put("image/bmp", "Bitmap image");\r
-        mimeType2HUmanReadable.put("image/gif", "GIF image");\r
-        mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");\r
-        mimeType2HUmanReadable.put("image/tiff", "TIFF image");\r
-        // music\r
-        mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");\r
-        mimeType2HUmanReadable.put("application/ogg", "OGG music file");\r
-\r
+    /**\r
+     * Converts Unix time to human readable format\r
+     * @param miliseconds that have passed since 01/01/1970\r
+     * @return The human readable time for the users locale\r
+     */\r
+    public static String unixTimeToHumanReadable(long milliseconds) {\r
+        Date date = new Date(milliseconds);\r
+        return date.toLocaleString();\r
     }\r
 }\r
     }\r
 }\r
index 7d10eba..f76c2b2 100644 (file)
@@ -135,6 +135,9 @@ public class FileDetailFragment extends SherlockFragment implements
             setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
                     .getMimetype()));\r
             setFilesize(mFile.getFileLength());\r
             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
                 try {\r
             // Update preview\r
             if (mFile.getStoragePath() != null) {\r
                 try {\r
@@ -178,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
     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
     }\r
 \r
     @Override\r