Added a static list of downloads in progress to have a reliable way to check if a...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / DisplayUtils.java
index bea19ac..3e9f778 100644 (file)
 \r
 package eu.alefzero.owncloud;\r
 \r
+import java.util.Date;\r
 import java.util.HashMap;\r
 \r
-import android.util.Log;\r
-\r
 /**\r
  * A helper class for some string operations.\r
+ * \r
  * @author Bartek Przybylski\r
- *\r
+ * \r
  */\r
 public class DisplayUtils {\r
-  public static String bitsToHumanReadable(long bitsLen) {\r
-    double result = bitsLen;\r
-    int attachedsuff = 0;\r
-    while (result > 1024 && attachedsuff < suffixes.length) {\r
-      result /= 1024.;\r
-      attachedsuff++;\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
-    result = ((int)(result * 100))/100.;\r
-    return result+suffixes[attachedsuff];\r
-  }\r
-  \r
-  public static String HtmlDecode(String s) {\r
-    String ret = "";\r
-    for (int i = 0; i < s.length(); ++i) {\r
-      if (s.charAt(i) == '%') {\r
-        ret += (char)Integer.parseInt(s.substring(i+1, i+3), 16);\r
-        i+=2;\r
-      } else {\r
-        ret += s.charAt(i);\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
+            attachedsuff++;\r
+        }\r
+        result = ((int) (result * 100)) / 100.;\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
+        /*\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
+                ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);\r
+                i += 2;\r
+            } else {\r
+                ret += s.charAt(i);\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
+        }\r
+        if (mimetype.split("/").length >= 2)\r
+            return mimetype.split("/")[1].toUpperCase() + " file";\r
+        return "Unknown type";\r
     }\r
-    return ret;\r
-  }\r
 \r
-  public static String convertMIMEtoPrettyPrint(String mimetype) {\r
-    if (mimeType2HUmanReadable.containsKey(mimetype)) {\r
-      return mimeType2HUmanReadable.get(mimetype);\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
-    return mimetype.split("/")[1].toUpperCase() + " file";\r
-  }\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