Merge pull request #186 from owncloud/fixed_contradicted_messages_in_login_view
[pub/Android/ownCloud.git] / src / com / owncloud / android / DisplayUtils.java
index a9cfe1d..d2939c8 100644 (file)
@@ -1,10 +1,10 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2011  Bartek Przybylski\r
+ *   Copyright (C) 2012-2013 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
- *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
- *   (at your option) any later version.\r
+ *   it under the terms of the GNU General Public License version 2,\r
+ *   as published by the Free Software Foundation.\r
  *\r
  *   This program is distributed in the hope that it will be useful,\r
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
 \r
 package com.owncloud.android;\r
 \r
+import java.util.Arrays;\r
 import java.util.Date;\r
 import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
 \r
 /**\r
  * A helper class for some string operations.\r
  * \r
  * @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\r
  */\r
 public class DisplayUtils {\r
-\r
-    private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
+    \r
+    private static String TAG = DisplayUtils.class.getSimpleName(); \r
+    \r
+    private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
 \r
     private static HashMap<String, String> mimeType2HUmanReadable;\r
     static {\r
@@ -48,6 +53,22 @@ public class DisplayUtils {
 \r
     }\r
 \r
+    private static final String TYPE_APPLICATION = "application";\r
+    private static final String TYPE_AUDIO = "audio";\r
+    private static final String TYPE_IMAGE = "image";\r
+    private static final String TYPE_TXT = "text";\r
+    private static final String TYPE_VIDEO = "video";\r
+    \r
+    private static final String SUBTYPE_PDF = "pdf";\r
+    private static final String[] SUBTYPES_DOCUMENT = { "msword", "mspowerpoint", "msexcel", \r
+                                                        "vnd.oasis.opendocument.presentation",\r
+                                                        "vnd.oasis.opendocument.spreadsheet",\r
+                                                        "vnd.oasis.opendocument.text"\r
+                                                        };\r
+    private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));\r
+    private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};\r
+    private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));\r
+    \r
     /**\r
      * Converts the file size in bytes to human readable output.\r
      * \r
@@ -57,12 +78,12 @@ public class DisplayUtils {
     public static String bytesToHumanReadable(long bytes) {\r
         double result = bytes;\r
         int attachedsuff = 0;\r
-        while (result > 1024 && attachedsuff < suffixes.length) {\r
+        while (result > 1024 && attachedsuff < sizeSuffixes.length) {\r
             result /= 1024.;\r
             attachedsuff++;\r
         }\r
         result = ((int) (result * 100)) / 100.;\r
-        return result + " " + suffixes[attachedsuff];\r
+        return result + " " + sizeSuffixes[attachedsuff];\r
     }\r
 \r
     /**\r
@@ -104,6 +125,58 @@ public class DisplayUtils {
             return mimetype.split("/")[1].toUpperCase() + " file";\r
         return "Unknown type";\r
     }\r
+    \r
+    \r
+    /**\r
+     * Returns the resource identifier of an image resource to use as icon associated to a \r
+     * known MIME type.\r
+     * \r
+     * @param mimetype      MIME type string.\r
+     * @return              Resource identifier of an image resource.\r
+     */\r
+    public static int getResourceId(String mimetype) {\r
+\r
+        if (mimetype == null || "DIR".equals(mimetype)) {\r
+            return R.drawable.ic_menu_archive;\r
+            \r
+        } else {\r
+            String [] parts = mimetype.split("/");\r
+            String type = parts[0];\r
+            String subtype = (parts.length > 1) ? parts[1] : "";\r
+            \r
+            if(TYPE_TXT.equals(type)) {\r
+                return R.drawable.file_doc;\r
+    \r
+            } else if(TYPE_IMAGE.equals(type)) {\r
+                return R.drawable.file_image;\r
+                \r
+            } else if(TYPE_VIDEO.equals(type)) {\r
+                return R.drawable.file_movie;\r
+                \r
+            } else if(TYPE_AUDIO.equals(type)) {  \r
+                return R.drawable.file_sound;\r
+                \r
+            } else if(TYPE_APPLICATION.equals(type)) {\r
+                \r
+                if (SUBTYPE_PDF.equals(subtype)) {\r
+                    return R.drawable.file_pdf;\r
+                    \r
+                } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {\r
+                    return R.drawable.file_doc;\r
+\r
+                } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {\r
+                    return R.drawable.file_zip;\r
+                }\r
+    \r
+            }\r
+            // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed\r
+        }\r
+\r
+        // default icon\r
+        return R.drawable.file;\r
+    }\r
+\r
+    \r
 \r
     /**\r
      * Converts Unix time to human readable format\r