DisplayUtils.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.utils;
  19. import java.util.Arrays;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. import com.owncloud.android.R;
  26. /**
  27. * A helper class for some string operations.
  28. *
  29. * @author Bartek Przybylski
  30. * @author David A. Velasco
  31. */
  32. public class DisplayUtils {
  33. //private static String TAG = DisplayUtils.class.getSimpleName();
  34. private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  35. private static HashMap<String, String> mimeType2HUmanReadable;
  36. static {
  37. mimeType2HUmanReadable = new HashMap<String, String>();
  38. // images
  39. mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
  40. mimeType2HUmanReadable.put("image/jpg", "JPEG image");
  41. mimeType2HUmanReadable.put("image/png", "PNG image");
  42. mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
  43. mimeType2HUmanReadable.put("image/gif", "GIF image");
  44. mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
  45. mimeType2HUmanReadable.put("image/tiff", "TIFF image");
  46. // music
  47. mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
  48. mimeType2HUmanReadable.put("application/ogg", "OGG music file");
  49. }
  50. private static final String TYPE_APPLICATION = "application";
  51. private static final String TYPE_AUDIO = "audio";
  52. private static final String TYPE_IMAGE = "image";
  53. private static final String TYPE_TXT = "text";
  54. private static final String TYPE_VIDEO = "video";
  55. private static final String SUBTYPE_PDF = "pdf";
  56. private static final String[] SUBTYPES_DOCUMENT = { "msword",
  57. "vnd.openxmlformats-officedocument.wordprocessingml.document",
  58. "vnd.oasis.opendocument.text",
  59. "rtf"
  60. };
  61. private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
  62. private static final String[] SUBTYPES_SPREADSHEET = { "msexcel",
  63. "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  64. "vnd.oasis.opendocument.spreadsheet"
  65. };
  66. private static Set<String> SUBTYPES_SPREADSHEET_SET = new HashSet<String>(Arrays.asList(SUBTYPES_SPREADSHEET));
  67. private static final String[] SUBTYPES_PRESENTATION = { "mspowerpoint",
  68. "vnd.openxmlformats-officedocument.presentationml.presentation",
  69. "vnd.oasis.opendocument.presentation"
  70. };
  71. private static Set<String> SUBTYPES_PRESENTATION_SET = new HashSet<String>(Arrays.asList(SUBTYPES_PRESENTATION));
  72. private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
  73. private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
  74. private static final String SUBTYPE_OCTET_STREAM = "octet-stream";
  75. private static final String EXTENSION_RAR = "rar";
  76. private static final String EXTENSION_RTF = "rtf";
  77. private static final String EXTENSION_3GP = "3gp";
  78. /**
  79. * Converts the file size in bytes to human readable output.
  80. *
  81. * @param bytes Input file size
  82. * @return Like something readable like "12 MB"
  83. */
  84. public static String bytesToHumanReadable(long bytes) {
  85. double result = bytes;
  86. int attachedsuff = 0;
  87. while (result > 1024 && attachedsuff < sizeSuffixes.length) {
  88. result /= 1024.;
  89. attachedsuff++;
  90. }
  91. result = ((int) (result * 100)) / 100.;
  92. return result + " " + sizeSuffixes[attachedsuff];
  93. }
  94. /**
  95. * Removes special HTML entities from a string
  96. *
  97. * @param s Input string
  98. * @return A cleaned version of the string
  99. */
  100. public static String HtmlDecode(String s) {
  101. /*
  102. * TODO: Perhaps we should use something more proven like:
  103. * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
  104. */
  105. String ret = "";
  106. for (int i = 0; i < s.length(); ++i) {
  107. if (s.charAt(i) == '%') {
  108. ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
  109. i += 2;
  110. } else {
  111. ret += s.charAt(i);
  112. }
  113. }
  114. return ret;
  115. }
  116. /**
  117. * Converts MIME types like "image/jpg" to more end user friendly output
  118. * like "JPG image".
  119. *
  120. * @param mimetype MIME type to convert
  121. * @return A human friendly version of the MIME type
  122. */
  123. public static String convertMIMEtoPrettyPrint(String mimetype) {
  124. if (mimeType2HUmanReadable.containsKey(mimetype)) {
  125. return mimeType2HUmanReadable.get(mimetype);
  126. }
  127. if (mimetype.split("/").length >= 2)
  128. return mimetype.split("/")[1].toUpperCase() + " file";
  129. return "Unknown type";
  130. }
  131. /**
  132. * Returns the resource identifier of an image resource to use as icon associated to a
  133. * known MIME type.
  134. *
  135. * @param mimetype MIME type string.
  136. * @param filename name, with extension
  137. * @return Resource identifier of an image resource.
  138. */
  139. public static int getResourceId(String mimetype, String filename) {
  140. if (mimetype == null || "DIR".equals(mimetype)) {
  141. return R.drawable.ic_menu_archive;
  142. } else {
  143. String [] parts = mimetype.split("/");
  144. String type = parts[0];
  145. String subtype = (parts.length > 1) ? parts[1] : "";
  146. if(TYPE_TXT.equals(type)) {
  147. return R.drawable.file_doc;
  148. } else if(TYPE_IMAGE.equals(type)) {
  149. return R.drawable.file_image;
  150. } else if(TYPE_VIDEO.equals(type)) {
  151. return R.drawable.file_movie;
  152. } else if(TYPE_AUDIO.equals(type)) {
  153. return R.drawable.file_sound;
  154. } else if(TYPE_APPLICATION.equals(type)) {
  155. if (SUBTYPE_PDF.equals(subtype)) {
  156. return R.drawable.file_pdf;
  157. } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
  158. return R.drawable.file_doc;
  159. } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) {
  160. return R.drawable.file_xls;
  161. } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) {
  162. return R.drawable.file_ppt;
  163. } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
  164. return R.drawable.file_zip;
  165. } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) {
  166. if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) {
  167. return R.drawable.file_zip;
  168. } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) {
  169. return R.drawable.file_doc;
  170. } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) {
  171. return R.drawable.file_movie;
  172. }
  173. }
  174. }
  175. }
  176. // default icon
  177. return R.drawable.file;
  178. }
  179. private static String getExtension(String filename) {
  180. String extension = filename.substring(filename.lastIndexOf(".") + 1);
  181. return extension;
  182. }
  183. /**
  184. * Converts Unix time to human readable format
  185. * @param miliseconds that have passed since 01/01/1970
  186. * @return The human readable time for the users locale
  187. */
  188. public static String unixTimeToHumanReadable(long milliseconds) {
  189. Date date = new Date(milliseconds);
  190. return date.toLocaleString();
  191. }
  192. public static int getSeasonalIconId() {
  193. if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354) {
  194. return R.drawable.winter_holidays_icon;
  195. } else {
  196. return R.drawable.icon;
  197. }
  198. }
  199. }